C# 在字符串中用引号执行 CMD 命令
C# Executing CMD command with quotation mark in string
我要执行以下cmd命令:
"C:\Program Files\bin\install332.exe" remove tap0901
这是我的 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);
但是cmd 命令将无法正常执行,因为cmd 命令中指定"install332.exe" 位置的引号没有出现。感谢您的帮助。
请试试这个:
string path = "\"C:\Program Files\bin\install332.exe\" remove tap0901";
Console.WriteLine(path);
结果应该是:
"C:\Program Files\bin\install332.exe" remove tap0901
Install332.Arguments = (@"""C:\Program Files\bin\install332.exe"" remove tap0901");
您给定的字符串中有五个引号。这意味着其中一个没有匹配,那就是最后一个额外的 "
。
Install332.Arguments = (""C:\Program Files\bin\install332.exe" remove tap0901");
最好使用 @
使您的字符串成为 verbatim string literal,因为您正在处理路径。
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");
我什至都懒得去 "cmd.exe"
Install332.FileName = (@"C:\Program Files\bin\install332.exe");
Install332.Arguments = ("remove tap0901");
这样您就不必担心其中包含 space 的路径周围的双引号,但您需要分隔反斜杠或使用逐字字符串,就像我在此处所做的那样.
我要执行以下cmd命令:
"C:\Program Files\bin\install332.exe" remove tap0901
这是我的 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);
但是cmd 命令将无法正常执行,因为cmd 命令中指定"install332.exe" 位置的引号没有出现。感谢您的帮助。
请试试这个:
string path = "\"C:\Program Files\bin\install332.exe\" remove tap0901";
Console.WriteLine(path);
结果应该是:
"C:\Program Files\bin\install332.exe" remove tap0901
Install332.Arguments = (@"""C:\Program Files\bin\install332.exe"" remove tap0901");
您给定的字符串中有五个引号。这意味着其中一个没有匹配,那就是最后一个额外的 "
。
Install332.Arguments = (""C:\Program Files\bin\install332.exe" remove tap0901");
最好使用 @
使您的字符串成为 verbatim string literal,因为您正在处理路径。
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");
我什至都懒得去 "cmd.exe"
Install332.FileName = (@"C:\Program Files\bin\install332.exe");
Install332.Arguments = ("remove tap0901");
这样您就不必担心其中包含 space 的路径周围的双引号,但您需要分隔反斜杠或使用逐字字符串,就像我在此处所做的那样.