我写了一个函数,它应该用引号包围一些参数,但似乎程序永远不会被调用。
让我感到惊讶的是,当我复制/粘贴控制台输出时,程序被调用就好了。
另外,如果我在for循环中所做的一切都超过了参数,那么它的效果很好。
知道我的错误在哪里?
public static bool callMacroProcess(String directory, String[] args, String process)
{
String realArgs = "";
String nextArg = "";
foreach (String arg in args)
{
if (arg.StartsWith("-p="))
{
String tmp = arg.Substring(3);
String argType = arg.Substring(0, 3);
if (!String.IsNullOrWhiteSpace(tmp))
{
realArgs += argType + "\"" + tmp + "\" ";
}
else
{
nextArg = argType + " ";
}
}
else if (!String.IsNullOrWhiteSpace(nextArg))
{
realArgs += nextArg + "\"" + arg + "\" ";
nextArg = "";
}
else
{
realArgs += arg + " ";
}
}
if (verbose)
{
Console.WriteLine("\"" + directory + process + "\" " + realArgs);
}
var proc = new System.Diagnostics.Process
{
StartInfo = new System.Diagnostics.ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "/C \"" + directory + process + "\" " + realArgs,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
return true;
}
答案 0 :(得分:2)
您使用ProcessStartInfo的方式不正确。请尝试以下选项之一。选项之间唯一的变化是/ c或/ k,如果没有尝试使用其他格式,请尝试。
示例:
运行程序并传递Filename参数: CMD / c write.exe c:\ docs \ sample.txt
运行程序并传递长文件名: CMD / c write.exe“c:\ sample documents \ sample.txt”
计划路径中的空格: CMD / c“”c:\ Program Files \ Microsoft Office \ Office \ Winword.exe“”
程序路径中的空格+参数: CMD / c“”c:\ Program Files \ demo.cmd“”Parameter1 Param2
程序路径中的空格+带空格的参数: CMD / k“”c:\ batch files \ demo.cmd“”带空格的参数1“”带有空格的参数2“”
启动Demo1然后启动Demo2:
CMD / c“”c:\ Program Files \ demo1.cmd“&”c:\ Program Files \ demo2.cmd“”
Process proc = new Process();
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo = new ProcessStartInfo("cmd", "/c " + directory + process + "\" " + realArgs);
proc.Start();
Process proc = new Process();
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo = new ProcessStartInfo("cmd", "/c " + directory + process + "\" " + realArgs);
proc.Start();
要么
Process proc = new Process();
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo = new ProcessStartInfo("cmd", "/k " + directory + process + "\" " + realArgs);
proc.Start();
答案 1 :(得分:0)
我将进程文件夹添加到PATH中。并成功尝试使用以下解决方案调用该程序:
public static bool callMacroProcess(String directory, String[] args, String process)
{
String realArgs = "";
String nextArg = "";
foreach (String arg in args)
{
if (arg.StartsWith("-p="))
{
String tmp = arg.Substring(3);
String argType = arg.Substring(0, 3);
if (!String.IsNullOrWhiteSpace(tmp))
{
realArgs += argType + "\"" + tmp + "\" ";
}
else
{
nextArg = argType + " ";
}
}
else if (!String.IsNullOrWhiteSpace(nextArg)) //si l'argument précédent était seul
{
realArgs += nextArg + "\"" + arg + "\" ";
nextArg = "";
}
else
{
realArgs += arg + " ";
}
}
if (verbose)
{
Console.WriteLine("Arguments en parametres : " + realArgs);
}
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.CreateNoWindow = false;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo = new System.Diagnostics.ProcessStartInfo(process, realArgs);
proc.Start();
return true;
}
我仍然无法理解为什么当我用引号包围一些参数时,试图用CMD调用该程序并不起作用。至少现在我可以用好的参数调用.exe。