将Cmd命令传递给C#应用程序 [英] Passing Cmd Command to C# Application

查看:86
本文介绍了将Cmd命令传递给C#应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a program that accepts a Cmd command as a command argument.

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

Basically you call it this way: C:\MyProgram.exe del C:\test.txt

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

The above command works fine. However when I try to do an xcopy command it fails:

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;
    }
}

推荐答案

我认为Jimmy Hoffa的答案是正确的,要解决该问题,您可以在命令的开头将"start"连接起来.

I think Jimmy Hoffa answer is right, to solve it you can cocatenate "start " at the beggining of the command.

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

xcopy C:\temp\test.html C:\temp\test2.html will bring you a window with a prompt when needed.

这篇关于将Cmd命令传递给C#应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆