How to Open Command Prompt Using C#.net & VB.Net

How to open a command prompt (cmd) in windows application using c#.net and vb.net. On button click open command prompt with c# code.
In today's article i will how you how you can open command prompt using c#.net & VB.net. So in some scenario while developing windows application we need to open the command prompt using c#.net and vb.net. 

In my previous article I have shown you Upload, Read and Display Excel File in .Net Core Using C#.net Windows ApplicationHow to Show Error & Warning MessageBox in C#.Net, Windows ApplicationEncrypt and Decrypt Strings in C#.Net & VB.NET With a Secret KeyRemove Password/ Decrypt Excel Using C#, VB.Net in Windows Application.

So, for this article first we will create a new windows application and add a windows form. In this form add a button control. On click on this button we will open the command prompt. 

Windows From With  Button

Now on button click add the below code. 

C#.Net

private void button1_Click(object sender, EventArgs e)
{
    string strCmdText;
    strCmdText = "command prompt";
    System.Diagnostics.Process.Start("CMD.exe", strCmdText);
}

VB. Net

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim strCmdText As String
    strCmdText = "command prompt"
    System.Diagnostics.Process.Start("CMD.exe", strCmdText)
End Sub 
In above code I have used Process.Start to open the command prompt. Now let's run the code to check the output. 

open cmd in c#.net

Click on button to open the command prompt.

cmd

Post a Comment