首次提交:添加src文件夹代码

This commit is contained in:
2026-02-27 14:02:43 +08:00
commit d330cfbca7
4184 changed files with 5546478 additions and 0 deletions

View File

@@ -0,0 +1,193 @@
using Cowain.Bake.Common.Core;
using FluentFTP;
using System;
using System.IO;
namespace Cowain.Bake.Communication.FTP
{
//现在是的是短连接,即上传完就关闭; 后期如果有效率上的要求,要改成长连接,就是过程中不关闭连接
//建立好连接700毫秒
//关闭连接90毫秒
//全部都是远程操作
public class FtpHelper
{
#region
public FtpClient ftpClient { get; set; } ///
public string IpAddr { get; set; } /// IP地址
public string RelatePath { get; set; } /// 相对路径
public int Port { get; set; } /// 端口号
public string UserName { get; set; } /// 用户名
public string Password { get; set; } /// 密码
public bool IsConnected { get; set; } = false;
//https://blog.csdn.net/fengershishe/article/details/129140618
public FtpHelper(string ipAddr, int port, string userName, string password, string relatePath = "")
{
this.IpAddr = ipAddr;
this.Port = port;
this.UserName = userName;
this.Password = password;
//this.RelatePath = relatePath;
}
#endregion
public bool Connect()
{
try
{
Close();
//using (var ftpClient = new FtpClient(this.IpAddr, this.UserName, this.Password, this.Port))
ftpClient = new FtpClient(this.IpAddr, this.Port);
ftpClient.Connect(); // 连接到FTP服务器
}
catch (Exception ex)
{
LogHelper.Instance.Error($"Ftp Connect {ex.Message}");
IsConnected = false;
return false;
}
IsConnected = ftpClient.IsConnected;
return IsConnected;
}
bool IsConnect()
{
if (null == ftpClient || !ftpClient.IsConnected || !IsConnected)
{
Connect();
}
return IsConnected;
}
public void Close()
{
if (null != ftpClient)
{
ftpClient.Disconnect();
ftpClient.Dispose();
ftpClient = null;
}
}
~FtpHelper()
{
Close();
}
#region
public FtpListItem[] ListDir()
{
FtpListItem[] lists = null;
if (IsConnect())
{
ftpClient.SetWorkingDirectory(this.RelatePath);
lists = ftpClient.GetListing(); //当前路径下,返回所有文件
}
return lists;
}
// 上传文件
public bool UpLoad(string srcFilePath, string descPath)
{
string descFilePath = null;
try
{
if (IsConnect())
{
string fileName = Path.GetFileName(srcFilePath);
char lastChar = descPath[descPath.Length - 1];
if ('\\' == lastChar
|| '/' == lastChar)
{
descFilePath = descPath + fileName; //descPath, 最后一个,是"/"
}
else
{
descFilePath = descPath + "/" + fileName; //descPath, 最后一个,是"/"
}
FtpStatus result = ftpClient.UploadFile(srcFilePath, descFilePath); // ("local/file.txt", "remote/file.txt");
return FtpStatus.Success == result ? true : false;
}
}
catch (Exception ex)
{
IsConnected = false;
LogHelper.Instance.Error($"Ftp UpLoad {ex.Message}");
}
return false;
}
///创建目录(远程创建)
private bool CheckDirIsExists(string dir)
{
bool flag = false;
try
{
if (IsConnect())
{
ftpClient.SetWorkingDirectory(this.RelatePath);
flag = ftpClient.DirectoryExists(dir);
if (!flag)
{
flag = ftpClient.CreateDirectory(dir); //client.CreateDirectory("remote/directory");
return flag;
}
}
return flag;
}
catch (Exception ex)
{
IsConnected = false;
LogHelper.Instance.Error($"Ftp CheckDirIsExists {ex.Message}");
}
return false;
}
public bool DeleteFile(string dir)
{
try
{
if (IsConnect())
{
ftpClient.SetWorkingDirectory(this.RelatePath);
ftpClient.DeleteFile(dir);
return true;
}
}
catch (Exception ex)
{
IsConnected = false;
LogHelper.Instance.Error($"Ftp DeleteFile {ex.Message}");
}
return false;
}
public bool DownloadFile(string localAddress, string remoteAddress)
{
try
{
if (IsConnect())
{
ftpClient.SetWorkingDirectory(this.RelatePath);
FtpStatus status = ftpClient.DownloadFile(localAddress, remoteAddress);
return status == FtpStatus.Success ? true : false;
}
}
catch (Exception ex)
{
IsConnected = false;
LogHelper.Instance.Error($"Ftp DownloadFile {ex.Message}");
}
return false;
}
#endregion
}
}

View File

@@ -0,0 +1,79 @@
using Cowain.Bake.Common;
using Cowain.Bake.Common.Core;
using System;
using System.IO;
using System.Timers;
using Unity;
namespace Cowain.Bake.Communication.FTP
{
//定时删除文件
public class FTPElapsedDelete
{
Timer timer;
string _deleteFilePath;
int _intervalDays;
public string Name { get; set; }
public IUnityContainer _unityContainer { get; set; }
public FTPElapsedDelete(IUnityContainer unityContainer)
{
_unityContainer = unityContainer;
}
public void Start(string filePath, int intervalDays)
{
_deleteFilePath = filePath;
_intervalDays = intervalDays;
CreateTimer();
}
void CreateTimer()
{
// 创建定时器并设置时间间隔为一天
//timer = new Timer();
timer = new Timer(TimeSpan.FromHours(12).TotalMilliseconds); //一天执行二次删除,一次就够了,怕晚上不上班
//timer.Interval = 10 * 1000; // 一个小时
timer.Elapsed += TimerElapsedDeleteFiles;
timer.Start();
}
public void Stop()
{
if (timer != null)
{
timer.Stop();
timer.Elapsed -= TimerElapsedDeleteFiles;
timer.Dispose();
timer = null;
}
}
//可配置定期删除日志文件
void TimerElapsedDeleteFiles(object sender, ElapsedEventArgs e)
{
if (Global.AppExit)
{
Stop();
}
string[] files = Directory.GetFiles(_deleteFilePath, "*", SearchOption.AllDirectories);
try
{
foreach (string file in files)
{
FileInfo fileInfo = new FileInfo(file); //后期有必要再多年是否上传属性,上传了就删除
TimeSpan timeDifference = DateTime.Now - fileInfo.CreationTime;
if (timeDifference.TotalDays >= _intervalDays)
{
File.SetAttributes(file, FileAttributes.Normal);
File.Delete(file); //"对路径“”的访问被拒绝"
}
}
}
catch (Exception ex)
{
LogHelper.Instance.Error($"删除文件失败,_deleteFilePath:{_deleteFilePath},异常:{ex.Message}");
}
}
}
}

View File

@@ -0,0 +1,108 @@
using Cowain.Bake.Common;
using Cowain.Bake.Common.Core;
using System;
using System.IO;
using System.Threading.Tasks;
using Unity;
using Unity.Resolution;
namespace Cowain.Bake.Communication.FTP
{
public class FTPUpload
{
string _localFolderPath;
string _remoteFolderPath;
FtpHelper ftpClient;
public string Name { get; set; }
public IUnityContainer _unityContainer { get; set; }
public FTPUpload(IUnityContainer unityContainer)
{
_unityContainer = unityContainer;
ftpClient = _unityContainer.Resolve<FtpHelper>(new ParameterOverride("ipAddr","10.19.30.19"), new ParameterOverride("port", 21),
new ParameterOverride("userName", (object)"admin"), new ParameterOverride("password", (object)"123456"));
ftpClient.Connect();
}
public void Stop()
{
ftpClient.Close();
}
public void Start(string src, string desc)
{
_localFolderPath = src;
_remoteFolderPath = desc;
Task.WaitAny();
Task.Run(async () =>
{
while(true)
{
await Task.Delay(1000);
if (Global.AppExit)
{
ftpClient.Close();
return;
}
UploadToFTPService();
}
});
}
//string localFolderPath = @"E:\svn\CutingStack\CuttingStackingMachine\成都蜂巢6074\tex"; //本地要上传的文件位置 (界面可配)
//string remoteFolderPath = "/"; //MOM服务要上传到的位置 (界面可配)
void UploadToFTPService()
{
// 获取文件夹及其子文件夹中的所有文件
string[] files = Directory.GetFiles(_localFolderPath, "*", SearchOption.AllDirectories);
//string[] files = Directory.GetFiles(localFolderPath);
try
{
// 遍历每个文件
foreach (string file in files)
{
if (IsUpload(file))
{
if (ftpClient.UpLoad(file, _remoteFolderPath))
{
SetFileReadAccess(file, true); //上传的文件做标识(设为可读)
}
}
}
}
catch(Exception ex)
{
LogHelper.Instance.Error($"上传文件失败,localFolderPath:{_localFolderPath},remoteFolderPath:{_remoteFolderPath},异常:{ex.Message}");
}
}
//是否上传:可读为假就上传,为真不上传
bool IsUpload(string filePath)
{
return !IsFileReadable(filePath); //默认为假,不可读(可写)
}
//读为可读
public void SetFileReadAccess(string filePath, bool readOnly)
{
FileInfo fInfo = new FileInfo(filePath);
fInfo.IsReadOnly = readOnly;
}
public bool IsFileReadable(string filePath)
{
try
{
FileAttributes attributes = File.GetAttributes(filePath);
bool isReadable = (attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly;
return isReadable;
}
catch (Exception ex)
{
Console.WriteLine($"判断文件可读性时出现错误: {ex.Message}");
return false;
}
}
}
}