Using the XCOPY command you can copy files and folders from one machine to another machine.
Here I will show you how we can execute XCOPY using C # but before showing that piece of code I would prefer to provide some explanations of XCOPY. So, here you go:
1. In computing, Xcopy is a command used in PC DOS, MS-DOS, OS/2, Microsoft Windows, and related operating systems for copying multiple files or entire directory trees from one directory to another and for copying files across a network. Xcopy stands for extended copy, and was created as a more functional file copying utility than the copy command found in these operating systems.
Source: http://en.wikipedia.org/wiki/Xcopy
To get more details about XCOPY you can visit the following link:
http://technet.microsoft.com/en-us/library/bb491035.aspx
Xcopy is more powerful than a simple copy function in .Net like-: Xcopy does a lot of things (buffering, error check, etc.) that are not easy to code by yourself, but you can always start a new process with Xcopy in the command line.
Here is the Code for the same:
|
using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading; namespace Xcopy { /// <summary> /// Use This class to Perform Xcopy /// </summary> class Program { /// <summary> /// This is your Main /// </summary> /// <param name="args"></param> static void Main(string[] args) { //Provide the Source location string sourceLoc = @"D:\BackUpFile\"; //Provide your Destination Location string destinationLoc = System.Environment.CurrentDirectory.Remove(System.Environment.CurrentDirectory.IndexOf("bin")) + "Source\\"; string FolderPath = "CopiedFiles"; if (!(Directory.Exists(destinationLoc + @"\" + FolderPath))) Directory.CreateDirectory(destinationLoc + @"\" + FolderPath); //Call a method to perform Xcopy ProcessXcopy(sourceLoc, destinationLoc + @"\" + FolderPath); Console.WriteLine("we are done with the xcopy"); } /// <summary> /// Method to Perform Xcopy to copy files/folders from Source machine to Target Machine /// </summary> /// <param name="SolutionDirectory"></param> /// <param name="TargetDirectory"></param> private static void ProcessXcopy(string SolutionDirectory, string TargetDirectory) { // Use ProcessStartInfo class ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.CreateNoWindow = false; startInfo.UseShellExecute = false; //Give the name as Xcopy startInfo.FileName = "xcopy"; //make the window Hidden startInfo.WindowStyle = ProcessWindowStyle.Hidden; //Send the Source and destination as Arguments to the process startInfo.Arguments = "\"" + SolutionDirectory + "\"" + " " + "\"" + TargetDirectory + "\"" + @" /e /y /I"; try { // Start the process with the info we specified. // Call WaitForExit and then the using statement will close. using (Process exeProcess = Process.Start(startInfo)) { exeProcess.WaitForExit(); } } catch (Exception exp) { throw exp; } } } } |

Asif IqbalPosted Jun 2, 2020, 2:51 AM
Nice solution, can u plz share if u find something good Xcopy Vs RoboCopy?
Anand GPosted Dec 5, 2014, 4:59 AM
hi sir, my requirement is to download excel file from ftp server to local machine. i tried with your code, my system hangs by displaying command prompt. How can i use your code to achieve my task, or else please suggest me how can i achieve this????
Ahmed AliPosted Apr 27, 2014, 3:55 AM
Dear sir This command doesn't copy the security
Ahmed AliPosted Apr 27, 2014, 3:37 AM
Really it is awesome article and the ifromation here was very useful for me, Thanks Jawd. :) :) :)
sank maheshPosted Nov 5, 2013, 4:43 AM
i need another solution.. i want to copy two location..xcopy src desc1 desc2..is that possible??
KrishPosted Feb 28, 2013, 6:32 AM
Thanks for sharing, quick question. I have implemented more or less similar process using vbScript, for some reason copying is much slower and uses less network bandwidth compared to copying from command using xcopy. Is this expected behaviour?
subhash sheladiaPosted Jul 5, 2012, 10:56 PM
good one.. helpful for me..
Csilla SZABO KOZELLPosted Feb 15, 2012, 1:13 AM
Excellent work. Compact and useful. Thank you for posting.
Abhi KumarPosted Feb 14, 2012, 11:24 PM
Nice article, Jawed. Thanks for sharing.
Monika AroraPosted Feb 14, 2012, 10:55 PM
Hi Jawed. Its a very nice and useful article.
Nitin SinghPosted Feb 14, 2012, 10:47 PM
Hi, Thanks for sharing and great stuff keep it up.