How to Execute a Command in CMD (Open Paint ‘mspaint.exe’) Using C#

Introduction

In this article we will show you how to execute a command in a command prompt also with Administrative Privilege using C# code. Since it is a demo application we are just going to execute a very simple command, "mspaint.exe", that will open Paint. By using this concept you can easily run any command you want to run in cmd by just using the C# code.

I would like to tell you one thing, that at least you should have some basic knowledge of the "ProcessStartInfo" class and the "Process" class.

If not then no need to worry, you can get it very easily on:

http://www.c-sharpcorner.com/UploadFile/2d2d83/how-to-start-a-process-like-cmd-window-using-C-Sharp/

Procedure

Step 1

Create a New “Windows Form Application” in Visual Studio and name it as you choose (I named it WriteCmdDemo).

Now a new form is created.

Step 2

Add a Button Control to your form and resize the window. Your form will look like this:


Step 3

Navigate to the code (Form1.cs) file and add the following two using directives to your project:

  1. using System.Diagnostics;  
  2. using System.IO; 

 

Step 4

Now just navigate to the Button control Click Event that you added to your project and add the following code:

  1. private void buttonOpenPaint_Click(object sender, EventArgs e)  
  2. {  
  3. ProcessStartInfo info = new ProcessStartInfo();  
  4. info.RedirectStandardError = true;  
  5. info.RedirectStandardInput = true;  
  6. info.RedirectStandardOutput = true;  
  7.   
  8. info.UseShellExecute = false;  
  9.   
  10. info.CreateNoWindow = true;  
  11.   
  12. /*-------- Start the CMD with the Administrtor 
  13. Previllages---------*/  
  14.   
  15. //Changing the Working Directory of the Process  
  16. //to System32 folder  
  17. info.WorkingDirectory = @"C:\Windows\System32";  
  18.   
  19. //Setting the FileName of the Process to CMD  
  20. info.FileName = @"C:\Windows\System32\cmd.exe";  
  21. info.Verb = "runas";  
  22. /*---------------------------------------------------*/  
  23.   
  24. Process pro = new Process();  
  25. pro.StartInfo = info;  
  26. pro.Start();  
  27.   
  28. //Setting the StreamWriter Class to write on CMD  
  29. StreamWriter sw = pro.StandardInput;  
  30.   
  31. //Checking whether it is allowed to write or not  
  32. if (sw.BaseStream.CanWrite)  
  33. {  
  34. try  
  35. {  
  36. //Writing on the Comand Line  
  37. //'mspaint.exe' which will  
  38. //open the Ms-Paint  
  39. sw.WriteLine("mspaint.exe");  
  40.   
  41. //Writing the Command to Exit the CMD  
  42. sw.WriteLine("exit");  
  43.   
  44. //Showing the Successful Message  
  45. MessageBox.Show("Ms-Paint is going to  
  46. Open!!!");  
  47.   
  48. }  
  49.   
  50. catch (Exception ex)  
  51. {  
  52. //If any Ocurred then the Message box is  
  53. // displayed  
  54. MessageBox.Show("An Error Occured Please  
  55. try again Later!");  
  56. }  
  57. }  

 

Step 5

Now compile and run your code, when you click on "Open Paint" the Microsoft Paint will be opened.

If you have any query regarding anything in this article then you can reply.

That's all for this article. I am embedding the source file so you can go through it.

Thank You.

Up Next
    Ebook Download
    View all
    Learn
    View all