Skip to content

Instantly share code, notes, and snippets.

@breezhang
Created March 26, 2012 10:44
Show Gist options
  • Save breezhang/2204394 to your computer and use it in GitHub Desktop.
Save breezhang/2204394 to your computer and use it in GitHub Desktop.
Shell Run in C#

cmd shell

PS shell

cmd Process

public bool CmdRun(Command cmd) { var proc = new System.Diagnostics.Process { EnableRaisingEvents = false, StartInfo = {FileName = cmd.FileName, Arguments = cmd.Argument} }; proc.Start(); proc.WaitForExit(); return MessageBox.Show("You have just visited www.microsoft.com") == DialogResult.OK; }

PS need powershell sdk >=2.0

public bool CmdTest(string cmd)
{
using (PowerShell powershell = PowerShell.Create().AddCommand(cmd))
{
Console.WriteLine(cmd + "              RUN");
Console.WriteLine("--------------------------------");
// Invoke the command synchronously and display the
// ProcessName and HandleCount properties of the // objects that are returned. foreach (PSObject result in powershell.Invoke()) { Console.WriteLine("{0}", result); } } return false; }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment