Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在我自己的控制台应用程序中执行命令提示符命令_C#_Console Application_Command Prompt - Fatal编程技术网

C# 如何在我自己的控制台应用程序中执行命令提示符命令

C# 如何在我自己的控制台应用程序中执行命令提示符命令,c#,console-application,command-prompt,C#,Console Application,Command Prompt,如何使我的控制台应用程序窗口的行为类似于命令提示符窗口,并执行我的命令行参数?我相信您正在寻找这一点 var command = "dir"; System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command); procStartInfo.RedirectStandardOutput = true; procStartInfo.Us

如何使我的控制台应用程序窗口的行为类似于命令提示符窗口,并执行我的命令行参数?

我相信您正在寻找这一点

var command = "dir";
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
Console.WriteLine(result);

这应该让你开始:

public class Program
{
    public static void Main(string[] args)
    {
        var proc = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName               = "cmd.exe",
                CreateNoWindow         = true,
                UseShellExecute        = false,
                RedirectStandardInput  = true,
                RedirectStandardOutput = true,
                RedirectStandardError  = true
            }
        };

        proc.Start();

        new Thread(() => ReadOutputThread(proc.StandardOutput)).Start();
        new Thread(() => ReadOutputThread(proc.StandardError)).Start();

        while (true)
        {
            Console.Write(">> ");
            var line = Console.ReadLine();
            proc.StandardInput.WriteLine(line);
        }
    }

    private static void ReadOutputThread(StreamReader streamReader)
    {
        while (true)
        {
            var line = streamReader.ReadLine();
            Console.WriteLine(line);
        }
    }
}
基本原则是:

  • 打开cmd.exe进程并捕获所有三个流(输入、输出、错误)
  • 从外到内传递输入
  • 读取输出并传输到您自己的输出
“重定向”选项很重要,否则您无法使用进程的各个流


上面的代码非常基本,但您可以对其进行改进。

有关这方面的在线资源很多。请在这里发布前进行研究。另外,谷歌搜索
c#console应用程序命令行参数的第一个结果是你们都误解了这个问题,请再读一遍。什么是命令提示符命令?什么是命令提示符窗口?您想将命令行参数传递给您的控制台应用程序还是什么?@WimOmbelets我认为编写shell包装器并不像您所展示的那么简单,这是一个值得问的问题。