Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.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# - Fatal编程技术网

C#命令不写一行

C#命令不写一行,c#,C#,你好,我如何让这个控制台写一行?我设法让它在你处理它时运行cmd.exe,但它不写行 private void button1_Click(object sender, EventArgs e) { if (textBox1.Text == "alpha") { progressBar1.Value = 100; if (progressBar1.Value == 100) {

你好,我如何让这个控制台写一行?我设法让它在你处理它时运行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!!!");
            }

        }

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

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

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

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();

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

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();

你没有入口点。你在哪里写这行代码?我想他想从winform应用程序启动一个命令行,然后将这行代码写到新的控制台。我如何让它工作?我是c#的新手。而且这不是全部代码,这只是代码中给我带来麻烦的部分。@AlinaB是的!我很好奇。。为什么希望WinForms应用程序写入命令控制台?为什么不把它附加到一些多行文本框中呢?你没有输入点。你在哪里写这行?我想他想从winform应用程序启动一个命令行,然后将这行写入新的控制台。我如何使它工作?我是c#的新手。而且这不是全部代码,这只是代码中给我带来麻烦的部分。@AlinaB是的!我很好奇。。为什么希望WinForms应用程序写入命令控制台?例如,为什么不把它附加到一些多行文本框中呢?