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

标签 c# command-line xcopy

我有一个程序接受 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;
    }
}

最佳答案

我认为您的应用程序正在运行,所以我尝试了该命令并得到了从 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)?

也许它是轰炸,因为您没有绑定(bind)标准输入,而您的应用只是从执行中返回返回代码。

关于c# - 将 Cmd 命令传递给 C# 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3426542/

相关文章:

c# - UWP 应用中的 WCF 发现

c# - 调整选定的 ListViewItem TextBlock 样式

batch-file - 批处理 : Copy files from txt file into one folder

windows - 使用xcopy复制所有desktop.ini

linux - 我如何连接到一个简单的 linux 控制台来执行一些任何人都可以使用 linux 操作系统的基本命令?

vb.net - VB.net 的 Xcopy 等效项

c# - 在 List<T> 中查找 IEnumerable<T> 时,GetGenericTypeDefinition 返回 false

c# - Kinect RGB 转 AVI 文件

flutter - 如何从命令行添加包?

shell - 当前目录中最后一个文件的全局别名