[英]Why does my call to another program from C# not work?
I wrote a function that is supposed to surround some arguments with quotation marks, but it seems the program is never called. 我写了一个应该用引号将一些参数括起来的函数,但是似乎从未调用过该程序。
What gets me is that when I copy/paste the console output, the program is called just fine. 我得到的是,当我复制/粘贴控制台输出时,该程序就很好了。
Also, if all I do in the for loop is past the arguments, then it works great. 另外,如果我在for循环中所做的所有事情都超出了参数,那么它就很好用。
Any idea where my mistake is ? 知道我的错误在哪里吗?
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;
}
The way you are riding your ProcessStartInfo is incorrect. 您乘坐ProcessStartInfo的方式不正确。 Try one of the options below.
请尝试以下选项之一。 The only thing that changes between the options is the / c or / k, with a try, if not try to work with the other format.
在选项之间唯一改变的是/ c或/ k,如果没有尝试使用其他格式,请尝试一下。
Examples: 例子:
Run a program and pass a Filename parameter: CMD /c write.exe c:\\docs\\sample.txt 运行一个程序并传递一个Filename参数:CMD / c write.exe c:\\ docs \\ sample.txt
Run a program and pass a Long Filename: CMD /c write.exe "c:\\sample documents\\sample.txt" 运行程序并传递长文件名:CMD / c write.exe“ c:\\ sample文件\\ sample.txt”
Spaces in Program Path: CMD /c ""c:\\Program Files\\Microsoft Office\\Office\\Winword.exe"" 程序路径中的空格:CMD / c“” c:\\ Program Files \\ Microsoft Office \\ Office \\ Winword.exe“”
Spaces in Program Path + parameters: CMD /c ""c:\\Program Files\\demo.cmd"" Parameter1 Param2 程序路径+参数中的空格:CMD / c“” c:\\ Program Files \\ demo.cmd“” Parameter1 Param2
Spaces in Program Path + parameters with spaces: CMD /k ""c:\\batch files\\demo.cmd" "Parameter 1 with space" "Parameter2 with space"" 程序路径+参数中带有空格的空格:CMD / k“” c:\\ batch files \\ demo.cmd“”参数1带有空格“” Parameter2带有空格“”
Launch Demo1 and then Launch Demo2: CMD /c ""c:\\Program Files\\demo1.cmd" & "c:\\Program Files\\demo2.cmd"" 启动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();
OR
要么
Process proc = new Process(); proc.StartInfo.CreateNoWindow = true; proc.StartInfo.UseShellExecute = false; proc.StartInfo = new ProcessStartInfo("cmd", "/k " + directory + process + "\\" " + realArgs); proc.Start();
I added the process folder to my PATH. 我将进程文件夹添加到了PATH中。 And successfully tried to call the program with the following solution :
并成功尝试使用以下解决方案调用该程序:
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;
}
I still can't understand why trying to call the program with CMD didn't work when I surrounded some arguments with quotation marks. 我仍然不明白为什么当我用引号将一些参数括起来时,尝试用CMD调用程序没有用。 At least now I can call the .exe with good arguments.
至少现在我可以使用良好的参数调用.exe。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.