Reader Level:
ARTICLE

XCOPY Using C# to Copy Files/Folders

Posted by Jawed MD Articles | .NET 4.5 February 14, 2012
This article shows the use of the XCOPY command to copy files and folders from one machine to another machine.
  • 0
  • 0
  • 42317
Download Files:
 

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;

            }

          

        }

    }

}

 

 


COMMENT USING

Trending up