Run command as if from DOS prompt

The following code snippet can be used to run a command as if you are opening a DOS prompt and typing it in.

public static string MyDOSMethod()
            ProcessStartInfo si = new ProcessStartInfo("cmd.exe");
            si.RedirectStandardInput = true;
            si.RedirectStandardOutput = true;
            si.UseShellExecute = false;

            Process p = Process.Start(si);
            p.StandardInput.WriteLine(@"cd \windows\system32");

                return p.StandardOutput.ReadToEnd();
            catch (Exception e)
                return e.Message;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s