2011-07-03 24 views
7

我试图将一个参数传递给一个进程名称为空格的文件夹。它不识别该文件夹。我怎样才能做到这一点?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); 

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

+0

尝试使用带有额外引号的my_arg编码,如my_arg =“\”“+ my_arg +”\“”;.这会起作用吗? – Gleno

回答

1

尝试做以下与空间foldernames应在cmd中被引用:

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

仍然无法正常工作? – pall

6

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

另一方面的空间需要特别小心 - 将参数包含在引号中可以解决此问题。

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

你忘记了结尾的引号。 – Guffa

2

试试这个:

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

请试试这个

串my_arg = @ “\” C:\ Program Files文件\我的文件夹空间\ |“;

的ProcessStartInfo proc = new ProcessStartInfo();

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

proc.Arguments = @my_arg;

Process.Start(proc);

相关问题