1

Well hello, how do I make this console write a line? I managed to make it so it run the cmd.exe when u process it, but it doesnt write the line.

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!!!");
            }

        }
Chris Loonam
  • 5,735
  • 6
  • 41
  • 63
Tomislav Tomi Nikolic
  • 608
  • 3
  • 10
  • 15

2 Answers2

8

if you want to interact with the console process, you need to do it like this :-

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();
Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
4

I'll assume this is a method that you somehow manage to call. The System.Diagnostics.Process.Start call will create a command box. However Console.WriteLine will try to write to whichever created your process (which is not the cmd.exe on the line above) and if it is a desktop app the call will have no console to write to, thus no message for you.

Otávio Décio
  • 73,752
  • 17
  • 161
  • 228