Search code examples
c#syntaxprocessstartinfo

Command Syntax for Processstartinfo from variables


Can someone please help me out with the syntax here

string sourcePath = @textBox2.Text.ToString();
string targetPath = @textBox1.Text.ToString();

System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(@"XCOPY C:\folder D:\Backup\folder /i");

psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
System.Diagnostics.Process copyFolders = System.Diagnostics.Process.Start(psi);
copyFolders.WaitForExit();

I am trying to change this line below to replace c:\folder with (sourcePath) and D:\Backup\folder with targetPath

(@"XCOPY C:\folder D:\Backup\folder /i");

I keep getting source not found when I messagebox like this it looks ok

MessageBox.Show(@"XCOPY " + (sourcePath) + (targetPath) + " /i");

Solution

  • You'll need to debug, I'd start here:

    string args = string.format("{0} {1} /i", sourcePath, targetPath);
    Debug.WriteLine(args);
    //verify your paths (both) are correct
    
    string cmd = string.format("XCOPY {0}", args);
    Debug.WriteLine(cmd);
    //copy the command and test it out directly in cmd
    

    Also, try passing your arguments....as arguments....

    System.Diagnostics.ProcessStartInfo psi = 
      new System.Diagnostics.ProcessStartInfo("XCOPY", args);
    

    You can look at the documentation for an example of passing with arguments