在Process.Start参数中传递引号

时间:2012-11-30 22:16:58

标签: .net batch-file

在.NET中我正在运行这一行

var p = Process.Start(@"cmd", @"/C mklink /H c:\z\b c:\z\a\");

这一切都很好但是我担心如果mklink的两个args中的一个有一个空间,这将无法正常工作。所以我在两个参数周围添加了“”。执行此行不再有效,当我写“\”而不是它仍然无效。

我在执行cmd /C时如何编写引号?

1 个答案:

答案 0 :(得分:3)

string sourcePath = @"c:\z\b";
string targetPath = @"c:\z\a";

string arguments = string.Format("\"{0}\" \"{1}\"", sourcePath, targetPath);

var p = Process.Start("cmd", "/C mklink /H " + arguments);

工作示例:

string sourcePath = @"c:\documents and settings\harvey robert\My Documents\Test.txt";
string targetPath = @"c:\test";

string s = string.Format("\"{0}\" \"{1}\"", sourcePath, targetPath);
Process.Start("cmd", @"/c copy " + s);

复制了1个文件。