将Cmd命令传递给C#应用程序

min*_*int 2 c# command-line xcopy

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

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

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

C:\MyProgram.exe xcopy C:\test.txt C:\Temp\Test2.txt
Run Code Online (Sandbox Code Playgroud)

程序代码:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Jim*_*ffa 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)?
Run Code Online (Sandbox Code Playgroud)

也许这是轰炸,因为你没有绑定stdin,你的应用程序只是从执行中返回返回代码.