问题描述
我的代码是
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在评论中正确指出的那样,您正在路径中转义"
。
由于某些特殊含义,某些字符编译器不会将其解释为字符串的一部分。
为了让编译器知道,您希望将那些字符解释为字符串的一部分,而必须将它们写成所谓的“转义序列”。
即这意味着在它们前面放一个反斜杠。
由于反斜杠本身也具有转义字符的特殊含义-如果希望将斜杠解释为字符串的一部分,则必须转义斜杠。
"\\"
将生成其中包含一个反斜杠的文字字符串,因为第一个反斜杠用于转义第二个反斜杠。
示例中的修复程序如下所示:
string programName = AppDomain.CurrentDomain.FriendlyName;
ProcessStartInfo proc = new ProcessStartInfo();
proc.FileName = "b.exe"
proc.Arguments = programName + "\\" + AppDomain.CurrentDomain.BaseDirectory + "\\";
Process.Start(proc);