c#命令不写一行

本文关键字:一行 命令 | 更新日期: 2023-09-27 18:09:50

你好,我如何让这个控制台写一行?我设法使它运行cmd.exe当你处理它,但它不写这一行。

private void button1_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "alpha")
        {
            progressBar1.Value = 100;
            if (progressBar1.Value == 100)
            {
                MessageBox.Show("Welcome back master!");
                System.Diagnostics.Process.Start(@"C:'Windows'System32'cmd.exe");
                Console.WriteLine("Hello!!!");
            }
        }

c#命令不写一行

如果您想与控制台进程交互,您需要这样做:-

var p = new Process
    {
        StartInfo =
            {
                FileName = "cmd.exe", 
                UseShellExecute = false, 
                RedirectStandardInput = true, 
                RedirectStandardOutput = true
            }
    };
p.Start();
var w = p.StandardInput;                
w.WriteLine("Dir");
w.WriteLine("Exit");            
var theDirectoryListing = p.StandardOutput.ReadToEnd();
p.WaitForExit();
w.Close();            
p.Close();

我假设这是一个您设法调用的方法。System.Diagnostics.Process.Start调用将创建一个命令框。然而控制台。WriteLine将尝试写入创建您的进程的任何进程(不是上面行的cmd.exe),如果它是桌面应用程序,则调用将没有控制台可写入,因此没有消息给您。