How To Start a Process CMD Using Console Application in Asp.Net



In this article we will build a very simple Windows Console Application that will start a process.
Create a new “Windows Console Application” in Visual Studio and name it as you choose. Now a new Program.cs is created.

Now add the following code.

static void Main(string[] args)
        {
            string myChoice;
            Console.WriteLine("Would you Like to Start CMD?");
            Console.WriteLine("\nPress Y(Yes) to start theCMD...");
            myChoice = Console.ReadLine();
            switch (myChoice)
            {
                case "Y":
                    StartProcess();
                    break;
                default:
                    Console.WriteLine("You have Enetered the Wrong Key.");
                    Console.ReadKey();
                    break;
            }
        }
private static void StartProcess()
        {
            ProcessStartInfo pro = new ProcessStartInfo();
            pro.FileName = "cmd.exe";
            //pro.WorkingDirectory = @"D:\MyTest";
            Process proStart = new Process();
            proStart.StartInfo = pro;
            proStart.Start();
        }

Compile your project and run it. You will see that when you enter your choice as "Y" in the Console window you will get the Cmd window appear.

To override this default path you need to just add a single line of code just before instantiating the Process class.

//Setting up the Working Directory
pro.WorkingDirectory = @"D:\MyTest";
Now after setting this, compile your project and run it.
Now when you enter your choice as "Y", you will get the CMD window open but with a different path.

Related Posts

Previous
Next Post »

Thanks for comments.....