SIGN UP MEMBER LOGIN:    
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.
Reader Level:
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;

            }

          

        }

    }

}

 

 


Login to add your contents and source code to this article
share this article :
post comment
 

Excellent work. Compact and useful. Thank you for posting.

Posted by Csilla SZABO KOZELL Feb 15, 2012

Nice article, Jawed. Thanks for sharing.

Posted by Abhi Kumar Feb 14, 2012

Hi Jawed. Its a very nice and useful article.

Posted by Monika Arora Feb 14, 2012

Hi, Thanks for sharing and great stuff keep it up.

Posted by Nitin Singh Feb 14, 2012
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor