Visual C#: Move multiple files with the same extensions into another directory

后端 未结 2 1864
悲&欢浪女
悲&欢浪女 2020-12-06 13:04

guys. I\'ve got a problem I can\'t solve: I have a 2 folders I choose with folderBrowserDialog and tons of files in source directory I need to move to the target directory.

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-06 13:15

    Try this:

    For copying files...

    foreach (string s in files)
    {
       File.Copy(s, "C:\newFolder\newFilename.txt");
    }
    

    for moving files

    foreach (string s in files)
    {
       File.Move(s, "C:\newFolder\newFilename.txt");
    }
    

    Example for Moving files to Directory:

    string filepath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    DirectoryInfo d = new DirectoryInfo(filepath);
    
    foreach (var file in d.GetFiles("*.txt"))
    {
          Directory.Move(file.FullName, filepath + "\\TextFiles\\" + file.Name);
    }
    

    will Move all the files from Desktop to Directory "TextFiles".

提交回复
热议问题