C#进程重定向输入未收到输入

我正在尝试使用C#执行come命令,这是我的代码:


        static void runCommand()
        {
            Process process = new Process();
            process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.Start();

            runSpecific(process,"dir");
            //runSpecific(process,"dir");

            process.WaitForExit();
        }

        static void runSpecific(Process process,string cmd)
        {
            process.StandardInput.WriteLine("/c "+cmd);
            Console.WriteLine("1");
            //process.StandardInput.Flush();
            //process.StandardInput.Close();
            string output = process.StandardOutput.ReadToEnd();
            Console.WriteLine("2");
            Console.WriteLine(output);
            Console.WriteLine("3");
            string err = process.StandardError.ReadToEnd();
            Console.WriteLine(err);
        }

I know about startInfo and I could have used it. But I don't want in that manner. I want to execute one command after another, like dir, cd xxx, dir like this, maybe from reading a text file. That's not the problem. The real problem is when I tried redirectInput and writeLine then. I even wrote "/c" even before my commands, but it doesn't output anything. For my type of debugging(Can't do that PRO thing), I added something to print, every few lines after. And I get only 0 and 1. So my assumption was right. There is something wrong in that line. I got no idea how to resolve that.

我还尝试过冲洗和关闭,但事与愿违,并崩溃了。

那么,我该如何解决呢?

评论