C# 参数 " 而不是反斜杠
C# argument " instead of backslash
我的代码是
a.exe
string programName = AppDomain.CurrentDomain.FriendlyName;
ProcessStartInfo proc = new ProcessStartInfo();
proc.FileName = "b.exe"
proc.Arguments = programName + " \"" + AppDomain.CurrentDomain.BaseDirectory + "\"";
Process.Start(proc)
并检查另一个程序的值
b.exe
MessageBox.Show(args[0]);
MessageBox.Show(args[1]);
我的预测值为
args[0] = a.exe
args[1] = D:\my test\with space\Folder\
但值为
args[0] = a.exe
args[1] = D:\my test\with space\Folder"
问题
BaseDirectory : C:\my test\with space\Folder\
so i cover BaseDirectory with " because has space.
as a result i want
b.exe a.exe "C:\my test\with space\Folder\"
but at b.exe check args[1] value is
D:\my test\with space\Folder"
where is my backslash and why appear "
请帮帮我...
正如 Kayndarr 已经在评论中正确指出的那样,您正在逃避 "
在您的路径中。
由于某些字符的特殊含义,编译器不会将其解释为字符串的一部分。
为了让编译器知道,您希望将这些字符解释为字符串的一部分,您必须将它们写在所谓的“escape-sequence”中。
即这意味着在它们前面加一个反斜杠。
因为反斜杠本身也有特殊的含义,如 escape-character - 如果您希望它被解释为字符串的一部分,您必须转义斜杠。
"\"
将生成一个带有 一个 反斜杠的文字字符串,因为第一个反斜杠用于转义第二个。
您的示例中的修复因此如下所示:
string programName = AppDomain.CurrentDomain.FriendlyName;
ProcessStartInfo proc = new ProcessStartInfo();
proc.FileName = "b.exe"
proc.Arguments = programName + "\" + AppDomain.CurrentDomain.BaseDirectory + "\";
Process.Start(proc);
我的代码是
a.exe
string programName = AppDomain.CurrentDomain.FriendlyName;
ProcessStartInfo proc = new ProcessStartInfo();
proc.FileName = "b.exe"
proc.Arguments = programName + " \"" + AppDomain.CurrentDomain.BaseDirectory + "\"";
Process.Start(proc)
并检查另一个程序的值
b.exe
MessageBox.Show(args[0]);
MessageBox.Show(args[1]);
我的预测值为
args[0] = a.exe
args[1] = D:\my test\with space\Folder\
但值为
args[0] = a.exe
args[1] = D:\my test\with space\Folder"
问题
BaseDirectory : C:\my test\with space\Folder\
so i cover BaseDirectory with " because has space.
as a result i want
b.exe a.exe "C:\my test\with space\Folder\"
but at b.exe check args[1] value is
D:\my test\with space\Folder"
where is my backslash and why appear "
请帮帮我...
正如 Kayndarr 已经在评论中正确指出的那样,您正在逃避 "
在您的路径中。
由于某些字符的特殊含义,编译器不会将其解释为字符串的一部分。
为了让编译器知道,您希望将这些字符解释为字符串的一部分,您必须将它们写在所谓的“escape-sequence”中。
即这意味着在它们前面加一个反斜杠。
因为反斜杠本身也有特殊的含义,如 escape-character - 如果您希望它被解释为字符串的一部分,您必须转义斜杠。
"\"
将生成一个带有 一个 反斜杠的文字字符串,因为第一个反斜杠用于转义第二个。
您的示例中的修复因此如下所示:
string programName = AppDomain.CurrentDomain.FriendlyName;
ProcessStartInfo proc = new ProcessStartInfo();
proc.FileName = "b.exe"
proc.Arguments = programName + "\" + AppDomain.CurrentDomain.BaseDirectory + "\";
Process.Start(proc);