0

I look at System.IO.File.Move, "move" the file to a new name.

System.IO.File.Move("oldfilename", "newfilename");

But it not enough for me to using this method.

Infact I want to recursively copy a folder and all of it's subfolders into a new path and change name of these file. anyone could take me a pieces of code?

  • To loop each sub-directory more safely and recursively, related topic: http://stackoverflow.com/questions/34525090/access-to-the-path-d-recycle-bin-s-1-5-21-494745725-312220573-749543506-41600/34525432#34525432 – Ian Feb 24 '16 at 15:50
  • This is very similar, but does not change the name of the files: http://stackoverflow.com/questions/58744/best-way-to-copy-the-entire-contents-of-a-directory-in-c-sharp – Matthew Watson Feb 24 '16 at 16:01

1 Answers1

0

If you want to move a folder:

string newDirPath = "E:\\New\\destDirName";
Directory.CreateDirectory(newDirPath);
Directory.Move("D:\\sourceDirPath", newDirPath);

If you want to copy a folder:

//Create all of the directories
foreach (string dirPath in Directory.GetDirectories(SourcePath, "*", 
    SearchOption.AllDirectories))
    Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath));

//Copy all the files & Replaces any files with the same name
foreach (string newPath in Directory.GetFiles(SourcePath, "*.*", 
    SearchOption.AllDirectories))
    File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath), true);
Community
  • 1
  • 1
NoName
  • 7,940
  • 13
  • 56
  • 108
  • 1
    Source: http://stackoverflow.com/questions/58744/best-way-to-copy-the-entire-contents-of-a-directory-in-c-sharp – Matthew Watson Feb 24 '16 at 16:02
  • using this command we can access to all folder and subfolders "foreach (string dirPath in Directory.GetDirectories(SourcePath, "*", SearchOption.AllDirectories))" okey ? –  Feb 24 '16 at 19:20
  • @MathLover yes, it is. – NoName Feb 24 '16 at 23:35