C# 中移动文件到指定位置
根据文件后缀名称将文件移动到指定的文件夹下面,具体代码如下:
demo中使用的是 .png 具体的情况根据你的需求可以更改
1 using System; 2 using System.IO; 3 4 public class FileMove 5 { 6 public FileMove() 7 { 8 // TODO: 9 } 10 11 // copy all file(*.png) in folder src to dest 12 private static void moveFiles(string srcFolder, string destFolder) 13 { 14 DirectoryInfo directoryInfo = new DirectoryInfo(srcFolder); 15 FileInfo[] files = directoryInfo.GetFiles(); 16 17 foreach (FileInfo file in files) // Directory.GetFiles(srcFolder) 18 { 19 if (file.Extension == ".png") 20 { 21 file.MoveTo(Path.Combine(destFolder, file.Name)); 22 } 23 // will move all files without if stmt 24 //file.MoveTo(Path.Combine(destFolder, file.Name)); 25 } 26 } 27 28 // test demo 29 static void Main(string[] args) 30 { 31 string src = "E:\\test\\src"; 32 string dest = "E:\\test\\dest"; 33 //moveFiles(dest, src); // dest -> src 34 moveFiles(src, dest); // src -> dest 35 Console.WriteLine("image copy finished!"); 36 Console.ReadLine(); 37 38 } 39 }