0

I want to move a folder containing 50GB of files to a Shared folder using C#. Iterating through directories and copying each files using FileInfo CopyTo method doesn't seems to be best option for me. I tried XCOPY Windows Command to copy. But I personally don't like the idea to call an external application which Microsoft may withdraw any time in future. My third option is to use Windows PowerShell cmdlet Move-Item.

Move-Item -Path D:\Files\Server-01 -Destination \\NAS-Storage-01\Backups\Server-01\ServerFiles

Can anybody tell me the pros and cons using this method using C# ?

Yesudass Moses
  • 1,841
  • 3
  • 27
  • 63

1 Answers1

4

xcopy.exe is a long-standing Windows console application that is not going anywhere.

These are a few options we have:

  • Directory.MoveTo: This does not work across different network paths.

  • Recursively move each files and subdirectories: This can take a long time if there are several files and subdirectories.

  • Use console application xcopy.exe; as the name suggests, however, only copying rather than moving is performed, so you'll need an additional step after successful copying that removes the source directory.

Here is sample code that invokes xcopy from C#, using Process.Start:

ProcessStartInfo Info = new ProcessStartInfo(); 
Info.Arguments = "/C xcopy D:\Files\Server-01 \\NAS-Storage-01\Backups\Server-01\ServerFiles"; 
Info.WindowStyle = ProcessWindowStyle.Hidden; 
Info.CreateNoWindow = true; 
Info.FileName = "cmd.exe"; 
Process.Start(Info);

Refer below links for more details on using xcopy in C#-

mklement0
  • 382,024
  • 64
  • 607
  • 775
Souvik Ghosh
  • 4,456
  • 13
  • 56
  • 78