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);
9
  • Why are you trying to get cmd to run the exe, rather than running the exe directly? Commented Dec 1, 2015 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. Commented Dec 1, 2015 at 12:20
  • why to execute Indirectly while you can execute exe directly Commented Dec 1, 2015 at 12:20
  • I am executing exe indirectly because only that way I can access it. Commented Dec 1, 2015 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? Commented Dec 1, 2015 at 12:25

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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\"";
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.
"/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'
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 :)

Comments

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""";

Comments

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.

1 Comment

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.