0

I have been trying to invoke below cmd command from C#, but it didn't worked and I got wrong path error. Although it is working if I execute it directly from CMD:

CMD Command: C:\Program Files (x86)\ABC Client>xyz.exe /launch "Your Software 12.7"

I tried below code:

ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd")
{
       WorkingDirectory = @"C:\Windows\System32",
       Arguments = "/C \"\"C:/Program Files (x86)/ABC Client/xyz.exe\"\" /launch 'Your Software 12.7'",
       RedirectStandardOutput = true,
       RedirectStandardError = true,
       WindowStyle = ProcessWindowStyle.Normal,
       UseShellExecute = false
};

Process process = Process.Start(processStartInfo);
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • Why are you trying to get cmd to run the exe, rather than running the exe directly? – Rowland Shaw Dec 01 '15 at 12:18
  • 1
    The slashes in your path are forward slashes, not backslashes, and the quotes around "Your Software 12.7" are singles, not doubles. – spender Dec 01 '15 at 12:20
  • why to execute Indirectly while you can execute exe directly –  Dec 01 '15 at 12:20
  • I am executing exe indirectly because only that way I can access it. – user5621234 Dec 01 '15 at 12:22
  • @spender The main problem is 'Your Software 12.7', It has to be in double quotes but if I put double quotes it invalidates the strings. Any idea how I can put the double quotes around it? – user5621234 Dec 01 '15 at 12:25
  • @user5621234 Yes, exactly the same way that you did it for the path of the executable. – spender Dec 01 '15 at 12:29
  • @spender I just changed it as per your suggestion but still my string is expecting ;. Below is the updated one `"/C \"\"C:/Program Files (x86)/ABC Client/xyz.exe\"\" /launch "\"Your Software 12.7\"";` – user5621234 Dec 01 '15 at 12:40
  • @spender I just changed it to `"/C \"\"C:/Program Files (x86)/ABC Client/xyz.exe\"\" /launch \"\"Your Software 12.7\"";` But not getting error The directory name is invalid. – user5621234 Dec 01 '15 at 12:44
  • http://stackoverflow.com/questions/6376113/spaces-problem-in-cmd – Antonín Lejsek Dec 01 '15 at 13:26

4 Answers4

1

You need to escape the quotation marks. This question is about escaping quotation marks

   string softwareName =  "\"Your Software 12.7\"";

This should do the trick.

Community
  • 1
  • 1
n_ananiev
  • 189
  • 1
  • 3
  • 12
  • Adding "\" around doesn't solved it: After adding "\" makes it invalidate: `"/C \"\"C:/Program Files (x86)/ABC Client/xyz.exe\"\" /launch "\"Your Software 12.7\"";` – user5621234 Dec 01 '15 at 12:39
  • I just changed it to `"/C \"\"C:/Program Files (x86)/ABC Client/xyz.exe\"\" /launch \"\"Your Software 12.7\"";` But not getting error The directory name is invalid. – user5621234 Dec 01 '15 at 12:45
  • "/C \"\"C:/Program Files (x86)/ABC Client/xyz.exe\"\" /launch \"Your Software 12.7\"" - this is your string with escaped quotation marks for 'Your Software 12.7' – n_ananiev Dec 01 '15 at 12:50
1

Finally fixed: The correct string will be:

Arguments = "/C \"\"C:/Program Files (x86)/ABC Client/xyz.exe\" /launch \"Your Software 12.7\"\"";

Thank you everyone for your inputs :)

0

If the problem is just the string to be executed, i think this outputs just what you want:

Arguments=@"C:\Program Files (x86)\ABC Client\xyz.exe /launch ""Your Software 12.7""";
Pikoh
  • 7,582
  • 28
  • 53
0

Either of the following should work:

Arguments = @"/C ""C:\Program Files (x86)\ABC Client\xyz.exe"" /launch ""Your Software 12.7""";

Arguments = "/C \"C:\\Program Files (x86)\\ABC Client\\xyz.exe\" /launch \"Your Software 12.7\"";

That is, double quotes around the program location (you had double quotes twice instead of once), back slashes instead of forward slashes, and double quotes around the "Your Software 27.7".

Using a string literal (@ prefix) you need a double quote before each double quote in the final string. Without the @ prefix, you need a back slash before each back slash and double quote in the final string.

Polyfun
  • 9,479
  • 4
  • 31
  • 39
  • It will not worked and give you an error: 'C:\Program' is not recognized as an internal or external command, operable program or batch file. I just cross checked it and found not working. It is due to the space in string path (Program Files, etc) – user5621234 Dec 01 '15 at 13:14