- This topic shows how to Create windows based DOS or Command Prompt in C#.
- With this example you can learn, how to read, write data to the command prompt and get data from command prompt and shows on the textbox in C#.
Design a form Create DOS or windows based Command Prompt in C# as shown in figure

Design form to create windows based Command Prompt or DOS in C#
- For this add three textboxes
1)Â Â Â Â Â txterror (get error from the command prompt or DOS) (Middle TextBox)
2)Â Â Â Â Â txtresult (get result from the command prompt or DOS) (Top TextBox)
3)Â Â Â Â Â txtcommand (write DOS command to execute) (Bottom TextBox)
System.Diagnostics Namespace – Create windows based DOS or Command Prompt in C#
- The System.Diagnostics namespace provides classes that allow you to interact with system processes, event logs, and performance counters.
- The System.Diagnostics namespace also provides classes that allow you to debug your application and to trace the execution of your code.
ProcessStartInfo Class – Create windows based DOS or Command Prompt in C#
- ProcessStartInfo Class specifies a set of values used when starting a process.
- ProcessStartInfo is used in conjunction with the Process component. When you start a process using the Process class, you have access to process information in addition to that available when attaching to a running process.
Write below code inside the form load – Create windows based DOS or Command Prompt in C#
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
ProcessStartInfo start_info = new ProcessStartInfo("cmd"); start_info.UseShellExecute = false; start_info.CreateNoWindow = true; start_info.RedirectStandardOutput = true; start_info.RedirectStandardError = true; start_info.RedirectStandardInput = true; // Make the process and set its start information. Process proc = new Process(); proc.StartInfo = start_info; // Start the process. proc.Start(); // Attach to stdout and stderr. StreamReader std_out = proc.StandardOutput; System.IO.StreamWriter SW = proc.StandardInput; StreamReader std_err = proc.StandardError; SW.WriteLine(txtcommand.Text); SW.WriteLine("exit"); // Display the results. txtresult.Text = std_out.ReadToEnd(); // Clean up. std_out.Close(); std_err.Close(); proc.Close(); |
Write below code inside the textbox keydown event (ie txtcommand_KeyDown) – Commant prompt in C#
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
if (e.KeyCode == Keys.Enter) { txterror.Text = ""; // Set start information. ProcessStartInfo start_info = new ProcessStartInfo("cmd"); start_info.UseShellExecute = false; start_info.CreateNoWindow = true; start_info.RedirectStandardOutput = true; start_info.RedirectStandardError = true; start_info.RedirectStandardInput = true; // Make the process and set its start information. Process proc = new Process(); proc.StartInfo = start_info; // Start the process. proc.Start(); // Attach to stdout and stderr. StreamReader std_out = proc.StandardOutput; System.IO.StreamWriter SW = proc.StandardInput; StreamReader std_err = proc.StandardError; SW.WriteLine(txtcommand.Text); SW.WriteLine("exit"); // Display the results. txtresult.Text = std_out.ReadToEnd(); txterror.Text = std_err.ReadToEnd(); std_err.Dispose(); SW.Close(); // Clean up. std_out.Close(); std_err.Close(); proc.Close(); } |
Create windows based DOS or Command Prompt in C# - Complete Code
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Diagnostics; namespace textc { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // Set start information. { ProcessStartInfo start_info = new ProcessStartInfo("cmd"); start_info.UseShellExecute = false; start_info.CreateNoWindow = true; start_info.RedirectStandardOutput = true; start_info.RedirectStandardError = true; start_info.RedirectStandardInput = true; // Make the process and set its start information. Process proc = new Process(); proc.StartInfo = start_info; // Start the process. proc.Start(); // Attach to stdout and stderr. StreamReader std_out = proc.StandardOutput; System.IO.StreamWriter SW = proc.StandardInput; StreamReader std_err = proc.StandardError; SW.WriteLine(txtcommand.Text); SW.WriteLine("exit"); // Display the results. txtresult.Text = std_out.ReadToEnd(); // Clean up. std_out.Close(); std_err.Close(); proc.Close(); } } private void txtcommand_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { txterror.Text = ""; // Set start information. ProcessStartInfo start_info = new ProcessStartInfo("cmd"); start_info.UseShellExecute = false; start_info.CreateNoWindow = true; start_info.RedirectStandardOutput = true; start_info.RedirectStandardError = true; start_info.RedirectStandardInput = true; // Make the process and set its start information. Process proc = new Process(); proc.StartInfo = start_info; // Start the process. proc.Start(); // Attach to stdout and stderr. StreamReader std_out = proc.StandardOutput; System.IO.StreamWriter SW = proc.StandardInput; StreamReader std_err = proc.StandardError; SW.WriteLine(txtcommand.Text); SW.WriteLine("exit"); // Display the results. txtresult.Text = std_out.ReadToEnd(); txterror.Text = std_err.ReadToEnd(); std_err.Dispose(); SW.Close(); // Clean up. std_out.Close(); std_err.Close(); proc.Close(); } } } } |

Create windows based Command Prompt or DOS in C# : Step 1

Create windows based Command Prompt or DOS in C# : Step 2

Create windows based Command Prompt or DOS in C# : Step 3
Create windows based DOS or Command Prompt in C#






