C# 实现:将一个文件夹下的.png图片全部移动到另一个文件夹

时间:2024-09-02 13:05:50

如题,代码如下:

using System;
using System.IO; public class FileMove
{
public FileMove()
{
// TODO:
} // copy all file(*.png) in folder src to dest
private static void moveFiles(string srcFolder, string destFolder)
{
DirectoryInfo directoryInfo = new DirectoryInfo(srcFolder);
FileInfo[] files = directoryInfo.GetFiles(); foreach (FileInfo file in files) // Directory.GetFiles(srcFolder)
{
if (file.Extension == ".png")
{
file.MoveTo(Path.Combine(destFolder, file.Name));
}
// will move all files without if stmt
//file.MoveTo(Path.Combine(destFolder, file.Name));
}
} // test demo
static void Main(string[] args)
{
string src = "E:\\test\\src";
string dest = "E:\\test\\dest";
//moveFiles(dest, src); // dest -> src
moveFiles(src, dest); // src -> dest
Console.WriteLine("image copy finished!");
Console.ReadLine(); }
}