0

I wanted to run CMD commands using C#. Therefore I copied some Code:

using System.IO;
using System.Diagnostics;

namespace Test
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Process p = new Process();
            ProcessStartInfo info = new ProcessStartInfo();
            info.FileName = "cmd.exe";
            info.RedirectStandardInput = true;
            info.UseShellExecute = false;

            p.StartInfo = info;
            p.Start();

            using (StreamWriter sw = p.StandardInput)
            {
                if (sw.BaseStream.CanWrite)
                {
                    sw.WriteLine("@echo off");
                    sw.WriteLine("title Test");
                }
            }
        }
    }
}

So. After I run some code like changing color etc, I want to enter normal cmd commands myself but the Window instantly closes. Thanks for any Help :D

  • Your program has no reason to stay open, so it closes. Give it a reason to stay on (e.g. read input). – ProgrammingLlama Jan 26 '22 at 08:24
  • 1
    Does this answer your question? [Why is the console window closing immediately once displayed my output?](https://stackoverflow.com/questions/8868338/why-is-the-console-window-closing-immediately-once-displayed-my-output) – Kevin Jan 26 '22 at 08:25

1 Answers1

0

Use ReadKey()

Console.ReadKey() Method makes the program wait for a key press and it prevents the screen until a key is pressed.

Indrit Kello
  • 1,293
  • 8
  • 19