问题:将文件夹及其下面的文件移动到其他目录
1 using Quality.Tools.Common.Log; 2 using System; 3 using System.Collections.Generic; 4 using System.Configuration; 5 using System.IO; 6 using System.Linq; 7 using System.Runtime.CompilerServices; 8 using System.Text; 9 using System.Threading.Tasks; 10 11 namespace RPA.MoveFiles 12 { 13 class Program 14 { 15 static List<string> pathList = new List<string>(); 16 static void Main(string[] args) 17 { 18 Console.WriteLine("Start..."); 19 try 20 { 21 Log4NetHelper.Info(typeof(Program), "Start..."); 22 23 var directoryPath = ConfigurationManager.AppSettings["DirectoryPath"]; 24 GetDirectoryPath(directoryPath); 25 26 if (pathList.Any()) 27 { 28 foreach (var item in pathList) 29 { 30 var backPath = item.Substring(0, item.LastIndexOf('\')).Replace("\Orchestrator\", "\Orchestrator-Archive\"); 31 CopyFolder(item, backPath); 32 33 DelectFiles(item); //Delete subdirectories and files 34 35 //string user = System.IO.File.GetAccessControl(item).GetOwner(typeof(System.Security.Principal.NTAccount)).ToString(); 36 //Log4NetHelper.Info(typeof(Program), "file:" + item + " User:" + user); 37 } 38 } 39 else 40 { 41 Log4NetHelper.Info(typeof(Program), "No files need to move..."); 42 } 43 Log4NetHelper.Info(typeof(Program), "Finished..."); 44 Console.WriteLine("Finished..."); 45 } 46 catch (Exception e) 47 { 48 Log4NetHelper.Error(typeof(Program), e.ToString()); 49 Console.WriteLine(e.ToString()); 50 } 51 Console.ReadKey(); 52 } 53 54 public static void GetDirectoryPath(string srcPath) 55 { 56 DirectoryInfo dir = new DirectoryInfo(srcPath); 57 FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //Returns all files and subdirectories in the directory 58 foreach (FileSystemInfo i in fileinfo) 59 { 60 if (i is DirectoryInfo && !i.Name.StartsWith("Robot")) //Determine whether folder 61 { 62 GetDirectoryPath(i.FullName); 63 } 64 else if (i is DirectoryInfo && i.Name.StartsWith("Robot")) 65 { 66 dir = new DirectoryInfo(i.FullName); 67 fileinfo = dir.GetFileSystemInfos(); //Returns all files and subdirectories in the directory 68 foreach (FileSystemInfo fileSystemInfo in fileinfo) 69 { 70 var createTime = fileSystemInfo.CreationTime; 71 72 if (createTime < DateTime.Now.AddMonths(-1)) //Delete files that are a month old 73 { 74 pathList.Add(fileSystemInfo.FullName); 75 } 76 } 77 } 78 } 79 } 80 81 public static void DelectFiles(string srcPath) 82 { 83 #region Delete the files or deirectories under the path 84 //DirectoryInfo dir = new DirectoryInfo(srcPath); 85 //FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //Returns all files and subdirectories in the directory 86 //foreach (FileSystemInfo i in fileinfo) 87 //{ 88 // if (i is DirectoryInfo) //Determine whether folder 89 // { 90 // DirectoryInfo subdir = new DirectoryInfo(i.FullName); 91 // subdir.Delete(true); //Delete subdirectories and files 92 // } 93 // else 94 // { 95 // //Before delete you must close the streamreader: sr.close(); 96 // File.Delete(i.FullName); //Delete specified file 97 // } 98 //} 99 #endregion 100 101 #region Delete the folder and the all contents under it. 102 if (Directory.Exists(srcPath)) //Determine whether folder 103 { 104 DirectoryInfo subdir = new DirectoryInfo(srcPath); 105 subdir.Delete(true); //Delete subdirectories and files 106 } 107 else if (File.Exists(srcPath)) //Determine whether folder 108 { 109 //Before delete you must close the streamreader: sr.close(); 110 File.Delete(srcPath); //Delete specified file 111 } 112 else 113 { 114 Log4NetHelper.Error(typeof(Program), "Deleted failed, invalid path."); 115 Console.WriteLine("Deleted failed, invalid path."); 116 } 117 #endregion 118 } 119 120 /// <summary> 121 /// Copy folders and files 122 /// </summary> 123 /// <param name="sourceFolder">Original file path</param> 124 /// <param name="destFolder">Destination file path</param> 125 /// <returns></returns> 126 public static void CopyFolder(string sourceFolder, string destFolder) 127 { 128 string folderName = System.IO.Path.GetFileName(sourceFolder); 129 string destfolderdir = System.IO.Path.Combine(destFolder, folderName); 130 string[] filenames = System.IO.Directory.GetFileSystemEntries(sourceFolder); 131 foreach (string file in filenames) // Go through all the files and directories 132 { 133 if (System.IO.Directory.Exists(file)) 134 { 135 string currentdir = System.IO.Path.Combine(destfolderdir, System.IO.Path.GetFileName(file)); 136 if (!System.IO.Directory.Exists(currentdir)) 137 { 138 System.IO.Directory.CreateDirectory(currentdir); 139 } 140 CopyFolder(file, destfolderdir); 141 } 142 else 143 { 144 string srcfileName = System.IO.Path.Combine(destfolderdir, System.IO.Path.GetFileName(file)); 145 if (!System.IO.Directory.Exists(destfolderdir)) 146 { 147 System.IO.Directory.CreateDirectory(destfolderdir); 148 } 149 System.IO.File.Copy(file, srcfileName); 150 } 151 } 152 } 153 } 154 }
App.config:
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <startup> 4 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 5 </startup> 6 <appSettings> 7 <add key="DirectoryPath" value="D:StudyOrchestratorStorage"/> 8 </appSettings> 9 </configuration>
上面的例子是将 D:StudyOrchestratorStorage 目录下一个月前的文件,移动到 D:StudyOrchestrator-ArchiveStorage 目录下,其中D:StudyOrchestrator ,D:StudyOrchestrator-Archive两个目录须存在。