C# - 将带有空格的参数传递给进程

时间:2011-07-03 10:31:28

标签: c#

我正在尝试将一个参数传递给一个进程,该文件夹的名称中包含空格。它无法识别该文件夹。我怎么能这样做?

string my_arg = @"C:\\program files\\my folder with spaces";

ProcessStartInfo proc = new ProcessStartInfo();

proc.FileName = @"C:\batches\my_batch.bat";

proc.Arguments = @my_arg ;

Process.Start(proc);

该过程无法启动 - 如果我使用名称中没有空格的文件夹,它确实有效。 谢谢!

4 个答案:

答案 0 :(得分:6)

你正在使用文字字符串;没有必要逃避反斜杠,事实上,如果你这样做,那么首先不需要使用文字字符串。

另一方面,空格需要特别小心 - 将参数包含在引号中解决了这个问题。

string my_arg = @"""C:\program files\my folder with spaces""";

答案 1 :(得分:2)

试试这个:

string my_arg = "\"C:\\program files\\my folder with spaces\"";

答案 2 :(得分:1)

尝试执行以下操作,因为带有空格的foldernames应引用cmd:

string my_arg = @"""C:\\program files\\my folder with spaces""";

答案 3 :(得分:0)

请试试这个

string my_arg = @“\”C:\ program files \ my folder with the space \ |“;

ProcessStartInfo proc = new ProcessStartInfo();

proc.FileName = @“C:\ batches \ my_batch.bat”;

proc.Arguments = @my_arg;

的Process.Start(PROC);