tags:

views:

525

answers:

2

Im using Process and ProcessStartInfo to launch a cmd window with the usual redirected output etc. However i'm tring to launch a command line executable with arguments, and even though the string is correct when i echo it to the cmd, when i run it without echo i get "'C:\Program' is not a recognized as an interal or external command" as if the path isn't in quotation marks and the space is throwing it off.

Any help greatley appreciated!

       static void Backup(string machinename)
        {
            Process p = new Process();
            p.StartInfo = new ProcessStartInfo("cmd", "/c \"C:\\Program Files\\Citrix\\XenCenter\\xe.exe\" vm-export vm=" + machinename + " -s 192.168.00.00 -u root -pw Password1! filename=\"C:\\VMs\\" + machinename + ".xva\"")
            {
                RedirectStandardOutput = true,
                UseShellExecute = false,
                CreateNoWindow = false
            };
            p.Start();
            string output = p.StandardOutput.ReadToEnd();
            p.WaitForExit();
            Console.WriteLine(output);
        }
A: 

You need to have surround paths with spaces with the extra quotes; but you should also use Environment.ExpandEnvironmentVariables instead of hard-code to "c".

Marc Gravell
+2  A: 

You shouldn't use "cmd" to call this - it's actually working against you in this case, and serves no purpose. Just call your executable directly:

p.StartInfo = new ProcessStartInfo(
    @"C:\Program Files\Citrix\XenCenter\xe.exe",
    "vm-export vm=" + machinename + " -s 192.168.00.00 -u root -pw Password1! filename=\"C:\\VMs\\" + machinename + ".xva\"")
        {
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = false
        };
Reed Copsey
Nailed it in one! Cheers!
Joe