Sometimes we need to run some system commands from within our program. In such cases the following example can be helpful.
<%-- Button in aspx file --%> <asp:Button ID="btnCommand" runat="server" Text="Button" onclick="btnCommand_Click" />
If you click on the button the following event handler will be called and the command line interface would be executed.
// Event handler protected void btnCommand_Click(object sender, EventArgs e) { System.Diagnostics.ProcessStartInfo objProcessStartInfo = new System.Diagnostics.ProcessStartInfo("CMD.exe", "/C " + "dir"); // CMD.exe is the name of the file which will execute. // /C is the path name. // dir is the command which will execute in command prompt. objProcessStartInfo.CreateNoWindow = true; // This line will not create any new window for command prompt. objProcessStartInfo.UseShellExecute = false; // Set UseShellExecute to specify whether to start the process by using the operating system shell. System.Diagnostics.Process objProcess = System.Diagnostics.Process.Start(objProcessStartInfo); objProcess.Close(); }
The above example will open the command line interface, but it will not be visible. If you want to make the command prompt visible then assign false to objProcessStartInfo.CreateNoWindow.