using System; using System.Configuration; using System.IO; using AutoFileCopyNet; using QMAPP.FJC.Entity.Basic; using Quartz; namespace QMTask.DataDeal { /// /// 复制文件服务 /// //并发限制 [DisallowConcurrentExecution] public class TempCopyFileJob : IJob { /// /// 执行方法 /// /// public virtual void Execute(IJobExecutionContext context) { try { //调用服务端方法 //var sa = new ServiceUtil().GetServiceAgent(); //sa.InvokeServiceFunction("TempProductBLL_TempCopyFile"); //var srcPath = @"\\192.168.0.202\temp"; //var destPath = @"D:\Test"; String srcPath = ConfigurationManager.AppSettings["SHARESERVER_IP"].ToString();//服务器目录 String destPath = ConfigurationManager.AppSettings["DCS_FILEPATH"].ToString();//目标目录 CopyDirectory( srcPath, destPath); context.Result = "复制文件服务开始:" + DateTime.Now.ToString("yy-MM-dd HH:mm:ss"); } catch (Exception ex) { context.Result = "复制文件服务失败" + ex; throw; } } public void CopyDirectory(string srcPath, string destPath) { DirectoryInfo dir = new DirectoryInfo(srcPath); FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //获取目录下(不包含子目录)的文件和子目录 foreach (FileSystemInfo i in fileinfo) { if (i is DirectoryInfo) //判断是否文件夹 { var fname = ""; if (i.Name.Length > 7) { fname = i.Name.Substring(0, 8); } var tname = DateTime.Now.AddDays(-1).ToString("yyyyMMdd"); //var nowName = DateTime.Now.ToString("yyyyMMdd"); if (fname == tname) { if (!Directory.Exists(destPath + "\\" + tname)) { Directory.CreateDirectory(destPath + "\\" + tname); //目标目录下不存在此文件夹即创建子文件夹 } ZipHandler.Zip(srcPath + "\\" + i.Name, destPath + "\\" + tname + "\\" + i.Name + ".zip", 5); //CopyFile(srcPath + "\\" + i.Name, destPath + "\\" + i.Name); } } } } public void CopyFile(string srcPath, string destPath) { DirectoryInfo dir = new DirectoryInfo(srcPath); FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //获取目录下(不包含子目录)的文件和子目录 foreach (FileSystemInfo i in fileinfo) { if (i is DirectoryInfo) //判断是否文件夹 { if (!Directory.Exists(destPath + "\\" + i.Name)) { Directory.CreateDirectory(destPath + "\\" + i.Name); //目标目录下不存在此文件夹即创建子文件夹 } CopyDirectory(i.FullName, destPath + "\\" + i.Name); //递归调用复制子文件夹 } else { File.Copy(i.FullName, destPath + "\\" + i.Name, true); //不是文件夹即复制文件,true表示可以覆盖同名文件 } } } } }