Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Practically anything you can run in the command line you can run in a C# program with similar constraints. There's a few ways to do it, one is via Asynchronous process commands as I show in my <a href="http://brianmackey.net/Blog/?p=10" rel="noreferrer">blog</a>. You just write and read to the command line in an active fashion. From here, just figure out what you want to accomplish and how to do it with a command line. Then plug it into the program</p> <pre><code>class Program { static void Main(string[] args) { LaunchCommandAsProcess cmd = new LaunchCommandAsProcess(); cmd.OutputReceived += new LaunchCommandAsProcess.OutputEventHandler(launch_OutputReceived); cmd.SendCommand("help"); cmd.SendCommand("ipconfig"); cmd.SyncClose(); } /// Outputs normal and error output from the command prompt. static void launch_OutputReceived(object sendingProcess, EventArgsForCommand e) { Console.WriteLine(e.OutputData); } } </code></pre> <p>As you can see, you simply instantiate the class, handle the output event, and start writing commands just like you were typeing into the command prompt. </p> <p>Here’s how it works:</p> <pre><code>public class LaunchCommandAsProcess { public delegate void OutputEventHandler(object sendingProcess, EventArgsForCommand e); public event OutputEventHandler OutputReceived; private StreamWriter stdIn; private Process p; public void SendCommand(string command) { stdIn.WriteLine(command); } public LaunchCommandAsProcess() { p = new Process(); p.StartInfo.FileName = @"C:\Windows\System32\cmd.exe"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; p.Start(); stdIn = p.StandardInput; p.OutputDataReceived += Process_OutputDataReceived; p.ErrorDataReceived += Process_OutputDataReceived; p.BeginOutputReadLine(); p.BeginErrorReadLine(); } /// /// Raises events when output data has been received. Includes normal and error output. /// /// /// private void Process_OutputDataReceived(object sendingProcess, DataReceivedEventArgs outLine) { if (outLine.Data == null) return; else { if (OutputReceived != null) { EventArgsForCommand e = new EventArgsForCommand(); e.OutputData = outLine.Data; OutputReceived(this, e); } } } /// /// Synchronously closes the command promp. /// public void SyncClose() { stdIn.WriteLine("exit"); p.WaitForExit(); p.Close(); } /// /// Asynchronously closees the command prompt. /// public void AsyncClose() { stdIn.WriteLine("exit"); p.Close(); } } public class EventArgsForCommand : EventArgs { public string OutputData { get; internal set; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload