C#実装:フォルダの下にある.pngピクチャはすべて別のフォルダに移動します

3660 ワード

問題のように、コードは次のとおりです.
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();
         
    }
}