1

I want to execute the following cmd command:

"C:\Program Files\bin\install332.exe" remove tap0901

This is my code in C#:

                ProcessStartInfo Install332= new ProcessStartInfo();
                path Install332.FileName = ("cmd.exe");
                //Our cmd code
                Install332.Arguments = (""C:\Program Files\bin\install332.exe" remove tap0901"");

                Install332.WindowStyle = ProcessWindowStyle.Hidden;
                Install332.CreateNoWindow = true;

                Process.Start(Install332);

But the cmd command won't execute properly since the quotation mark in the cmd command which specifies the "install332.exe" location don't appear. I appreciate any help.

John Doe
  • 13
  • 1
  • 3
  • Did you try by replacing the first and last quotation mark by an apostroph ? Like this : Install332.Arguments = ('"C:\Program Files\bin\install332.exe" remove tap0901'); You could also escape the inner apostrophes by adding a \ before ("\"C:\Program Files\bin\install332.exe" remove tap0901\"") – Michaël Polla Dec 01 '16 at 18:28
  • You need to delimit the quotes and the back slashes. Though why not make the File name the path to install332.exe and then the arguments "remove tap0901" instead? – juharr Dec 01 '16 at 18:29
  • 1
    Do you have a specific need to launch `cmd.exe` and instruct it to, in turn, launch `install332.exe`? Otherwise it would simplify things to just launch `install332.exe` directly. By the way, you probably want your first argument to `cmd.exe` to be `/C` or `/K`, and then subsequent arguments contain the command to execute. – Lance U. Matthews Dec 01 '16 at 18:34
  • Possible duplicate of [Spaces in file path passed as command line argument](http://stackoverflow.com/questions/27598563/spaces-in-file-path-passed-as-command-line-argument) – Lance U. Matthews Dec 01 '16 at 18:46

4 Answers4

3

I wouldn't even bother with "cmd.exe"

Install332.FileName = (@"C:\Program Files\bin\install332.exe");
Install332.Arguments = ("remove tap0901");

This way you don't have to worry about double quotes around the path that has a space in it, but you will need to either delimit the backslashes or use a verbatim string as I have done here.

juharr
  • 31,741
  • 4
  • 58
  • 93
2

Please try this:

string path = "\"C:\\Program Files\\bin\\install332.exe\" remove tap0901";
Console.WriteLine(path);

Result should be:

"C:\Program Files\bin\install332.exe" remove tap0901
kat1330
  • 5,134
  • 7
  • 38
  • 61
0
Install332.Arguments = (@"""C:\Program Files\bin\install332.exe"" remove tap0901");
Cetin Basoz
  • 22,495
  • 3
  • 31
  • 39
0

You have five quotation marks in your given string. Which means one of them doesn't have a match and that is the one extra " at the end.

Install332.Arguments = (""C:\Program Files\bin\install332.exe" remove tap0901");

Better use @ to make your string a verbatim string literal, since you are dealing with paths.

Verbatim string literals start with @ and are also enclosed in double quotation marks.

The advantage of verbatim strings is that escape sequences are not processed, which makes it easy to write.

Install332.Arguments = (@"""C:\Program Files\bin\install332.exe"" remove tap0901");
Community
  • 1
  • 1
raymelfrancisco
  • 828
  • 2
  • 11
  • 21
  • 3
    I think you will find that if you want a double quote in a verbatim string literal it still needs doubling up so you would need _@"""C:\Program Files\bin\install332.exe"" remove tap0901"_ – PaulF Dec 01 '16 at 18:41
  • 2
    double quotes always have to be delimited either by backslashes in a regular string or by another double quote in a verbatim string. – juharr Dec 01 '16 at 18:43
  • Edited my post, my bad. – raymelfrancisco Dec 01 '16 at 18:45