项目中有一部分核心代码是用C++控制台来写的。需要在.net中去调用。本以为是很简单的事情,但是中间却有一个非技术性但是却又没有头绪的问题困扰了我一天,现在终于云开雾散,特在此记述纪念这一天的时间。
调用C++使用的是.net中的Process类。代码如下
if (dosCommand != null && dosCommand != " ")
{
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/c " + dosCommand;//设定参数,其中的“/”表示执行完命令后马上退出
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = false;
startInfo.RedirectStandardOutput = true;
startInfo.CreateNoWindow = true;
process.StartInfo = startInfo;
try
&nb

在.NET中使用Process类调用CMD时,如果参数路径含有空格,会导致调用失败。通过在含有空格的参数路径前后添加双引号,并确保参数符合CMD的引号处理规则,可以成功解决此问题。此外,还可以直接调用目标程序,避免CMD作为中介,简化参数传递。

1054

被折叠的 条评论
为什么被折叠?



