Copy Folders in C# using System.IO

后端 未结 9 893
傲寒
傲寒 2020-12-10 03:20

I need to Copy folder C:\\FromFolder to C:\\ToFolder

Below is code that will CUT my FromFolder and then will create my ToFolder. So my FromFolder will be gone and al

9条回答
  •  被撕碎了的回忆
    2020-12-10 04:13

    This link provides a nice example.

    http://msdn.microsoft.com/en-us/library/cc148994.aspx

    Here is a snippet

    // To copy all the files in one directory to another directory.
    // Get the files in the source folder. (To recursively iterate through
    // all subfolders under the current directory, see
    // "How to: Iterate Through a Directory Tree.")
    // Note: Check for target path was performed previously
    //       in this code example.
    if (System.IO.Directory.Exists(sourcePath))
    {
      string[] files = System.IO.Directory.GetFiles(sourcePath);
    
      // Copy the files and overwrite destination files if they already exist.
      foreach (string s in files)
      {
        // Use static Path methods to extract only the file name from the path.
        fileName = System.IO.Path.GetFileName(s);
        destFile = System.IO.Path.Combine(targetPath, fileName);
        System.IO.File.Copy(s, destFile, true);
      }
    }
    

提交回复
热议问题