简体   繁体   English

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

[英]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.

相关问题 如果我在 C# 中的 Main 方法是私有的,为什么我的程序可以工作? - Why does my program work if my Main method in C# is private? 为什么我的 C# API 在服务器上工作正常,但当我尝试从任何其他客户端计算机调用它时总是失败? - Why does my C# API work fine on the server but it always fails when I try to call it from any other client machine? 为什么我的ReadProcessMemory()不起作用? (C#) - Why does my ReadProcessMemory() not work? (C#) 从另一个.NET应用程序调用时,我的WebService c#无法正常工作 - My WebService c# doesn't work when I call it from another .NET application 从Windows 7命令行运行时,为什么我的C#程序会经常暂停? - Why does my C# program pause so often when running from the Windows 7 command line? 为什么我的简单C#程序立即退出? - Why does my simple C# program quit immediately? 为什么我的C#程序无法识别长度? - Why does my C# program not recognize length? C# WPF 为什么我的程序在其他计算机上崩溃? - C# WPF Why Does my program crash on other computers? 为什么我的C#Windows窗体中的代码不起作用? - Why does my code in my c# windows form not work? 如何从C#中的第一个程序打开另一个程序? - How to open another program from my first in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM