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.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
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> internal class Program { /// <summary> /// This is your Main /// </summary> /// <param name="args"></param> private 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; } } } } |