2

我有一个接受 Cmd 命令作为命令参数的程序。

基本上你这样称呼它:C:\MyProgram.exe del C:\test.txt

上面的命令工作正常。但是,当我尝试执行 xcopy 命令时,它失败了:

C:\MyProgram.exe xcopy C:\test.txt C:\Temp\Test2.txt

程序代码:

class Program
{
    static void Main(string[] args)
    {
        string command = GetCommandLineArugments(args);

        // /c tells cmd that we want it to execute the command that follows and then exit.
        System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", @"/D /c " + command);

        procStartInfo.RedirectStandardOutput = true;
        procStartInfo.UseShellExecute = false;

        // Do not create the black window.
        procStartInfo.CreateNoWindow = true;
        procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;

        System.Diagnostics.Process process = new System.Diagnostics.Process();
        process.StartInfo = procStartInfo;
        process.Start();
    }

    private static string GetCommandLineArugments(string[] args)
    {
        string retVal = string.Empty;

        foreach (string arg in args)
            retVal += " " + arg;

        return retVal;
    }
}
4

2 回答 2

6

我认为您的应用程序正在运行,所以我尝试了该命令,并得到了来自 stdin 的输入提示:

C:\>xcopy C:\temp\test.html C:\temp\test2.html
Does C:\temp\test2.html specify a file name
or directory name on the target
(F = file, D = directory)?

可能是因为你没有绑定标准输入,而你的应用程序只是从执行返回返回码。

于 2010-08-06T18:22:01.580 回答
2

我认为 Jimmy Hoffa 的回答是正确的,要解决它,您可以在命令的开头将“start”连接起来。

xcopy C:\temp\test.html C:\temp\test2.html 将在需要时为您提供一个带有提示的窗口。

于 2010-08-06T18:27:30.023 回答