none
Copying a file RRS feed

  • Question

  • I created a console application to copy files from one source to a destination.

    static void Main(string[] args)
            {
                try
                {
                    //string strCommand = @"xcopy /-y E:\NHB_FTPS\*.* Z:\ /s /h /e/k/f/c/y/r";
    
                    string strSource = @"/-y F:\Maverick\Source\*.* ";
                    string strDestination = @" F:\Maverick\Destination\ ";
                    string strBatchCommand = @"xcopy " + strSource + strDestination + @" /s /h /e/k/f/c/y/r";
    
                    Process.Start(strBatchCommand, "Back up Process");
                }
                catch (Exception Exp)
                {
                    Console.WriteLine(Exp.Message);
                    Console.ReadKey();
                }
            }
    Despite the fact that the source and destination folder exists, why is it displaying this error "The system cannot find the file specified"

    Monday, October 9, 2017 6:50 AM

All replies

  • Try this:

    string strBatchCommand = strSource + strDestination + @" /s /h /e/k/f/c/y/r";
    Process.Start( "xcopy", strBatchCommand );
    

    • Proposed as answer by Fei HuModerator Wednesday, October 11, 2017 11:44 AM
    Monday, October 9, 2017 7:49 AM

  • Hi maverick786us1,

    I have made a sample on my side. The following code for your reference.

                //  Source location 
                string sourceLoc = @"D:\BackUpFile\*.*";
                //  Destination Location 
                string destinationLoc = @"D:\CopiedFile\";
                string FolderPath = "CopiedFiles";
                if (!(Directory.Exists(destinationLoc + @"\" + FolderPath)))
                    Directory.CreateDirectory(destinationLoc + @"\" + FolderPath);
                //Call a method to perform Xcopy 
                ProcessXcopy(sourceLoc, destinationLoc + @"\" + FolderPath);
                Console.WriteLine("Done Ok");
    
    
    
            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;
                }
    
            }
    


    To get more details about XCOPY you can visit the following link:
    http://technet.microsoft.com/en-us/library/bb491035.aspx


    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 10, 2017 9:54 AM
    Moderator