Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
473 views
in Technique[技术] by (71.8m points)

asp.net - Copy Folders in C# using System.IO

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 all the items will be in the newly created folder called ToFolder

System.IO.Directory.Move(@"C:FromFolder ", @"C:ToFolder");

But i just want to Copy the files in FromFolder to ToFolder. For some reason there is no System.IO.Directory.Copy???

How this is done using a batch file - Very easy

xcopy C:FromFolder C:ToFolder

Regards Etienne

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...