Execute a shell command from a .NET application

RCIX picture RCIX · Nov 30, 2009 · Viewed 8.5k times · Source

I need to execute a shell command from my .NET application, not unlike os.execute (a little way down on that page) in Lua. However with a cursory search I couldn't find anything. How do I do it?

Answer

data picture data · Nov 30, 2009
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "blah.lua arg1 arg2 arg3";
p.StartInfo.UseShellExecute = true;
p.Start();

Another way would be to use P/Invoke and use ShellExecute directly:

[DllImport("shell32.dll")]
static extern IntPtr ShellExecute(
    IntPtr hwnd,
    string lpOperation,
    string lpFile,
    string lpParameters,
    string lpDirectory,
    ShowCommands nShowCmd);