我正在为Application.ExecutablePath获取一个字符串变量,然后通过带有一些参数的ProcessStartInfo将该变量传递给命令行。只要“ExecutablePath”不包含空格,我的代码就可以正常工作,但是如果它像C:\ documents和settings \ jsmith \ desktop那样,C#将空格处理为转义,我不会从命令行获得结果
以下是我如何获取Path并发送命令“lmutil”
string execName = Application.ExecutablePath;
FileInfo execFileInfo = new FileInfo(execName);
execPath = execFileInfo.DirectoryName;
string lmUtil = @"\lmutil";
lmExec = execPath + lmUtil;
This is the method in which I need to handle quotes in paths
GetLicStats(lmExec + " lmstat -a -c " + licport + "@" + curAdd);
如果lmExec包含空格,我怎样才能确保正确处理lmExec。
答案 0 :(得分:3)
GetLicStats(lmExec + " lmstat -a -c " + licport + "@\"" + curAdd + "\"");
在地址周围加上引号通常有效
答案 1 :(得分:0)
execPath = execFileInfo.DirectoryName.Replace(" ", "\ ");
只需用转义空格替换空格。