繁体   English   中英

为什么我从C#调用另一个程序不起作用?

[英]Why does my call to another program from C# not work?

我写了一个应该用引号将一些参数括起来的函数,但是似乎从未调用过该程序。

我得到的是,当我复制/粘贴控制台输出时,该程序就很好了。

另外,如果我在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;
}

您乘坐ProcessStartInfo的方式不正确。 请尝试以下选项之一。 在选项之间唯一改变的是/ c或/ k,如果没有尝试使用其他格式,请尝试一下。

例子:

运行一个程序并传递一个Filename参数:CMD / c write.exe c:\\ docs \\ sample.txt

运行程序并传递长文件名:CMD / c write.exe“ c:\\ sample文件\\ 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带有空格“” Parameter2带有空格“”

启动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", "/k " + directory + process + "\\" " + realArgs); proc.Start(); 

我将进程文件夹添加到了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。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM