I need to execute a shell command from my .NET app, not unlike os.execute
(a little ways down on that page) in Lua. However with a cursory search i couldn't find anything, how do i do it?
views:
196answers:
3
+5
A:
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "blah.lua arg1 arg2 arg3";
p.StartInfo.UseShellExecute = true;
p.Start();
Other way would be to use pinvoke and use ShellExecute directly
[DllImport("shell32.dll")]
static extern IntPtr ShellExecute(
IntPtr hwnd,
string lpOperation,
string lpFile,
string lpParameters,
string lpDirectory,
ShowCommands nShowCmd);
data_smith
2009-11-30 02:15:15
I need to execut a lua script...
RCIX
2009-11-30 02:15:54
@RCIX: How did you do it now? The manual way I mean.
o.k.w
2009-11-30 02:17:01
i.e. putting `blah.lua somearg anotherarg thirdarg` in a console command.
RCIX
2009-11-30 02:17:03
+1
A:
There is an easy way to handle this in C#. Using the System.Diagnostics namespace, there is a class to handle spawning processes.
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "App.exe";
process.StartInfo.Arguments = "arg1 arg2 arg3";
process.Start();
Console.WriteLine(process.StandardOutput.ReadToEnd();
There are additional parameters to handle things such as not creating a console window, redirecting input or output, and most anything else you'd need.
MattGWagner
2009-11-30 02:33:44
+2
A:
You might want to consider an asynchronous approach if the script takes a while.
Here's some code which does that plus redirects standard output to capture for display on a form (WPF, winform, whatever). Note that I'm assuming you don't need user-input, so it doesn't create the console window, which looks better:
BackgroundWorker worker = new BackgroundWorker();
...
// wire up event in ctor or wherever is appropriate
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
...
// then to execute your script
worker.RunWorkerAsync("somearg anotherarg thirdarg");
void worker_DoWork(object sender, DoWorkEventArgs e)
{
StringBuilder result = new StringBuilder();
Process process = new Process();
process.StartInfo.FileName = "blah.lua";
process.StartInfo.Arguments = (string)e.Argument;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
process.Start();
result.Append(process.StandardOutput.ReadToEnd());
process.WaitForExit();
e.Result = result.AppendLine().ToString();
}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Result != null) console.Text = e.Result.ToString();
else if (e.Error != null) console.Text = e.Error.ToString();
else if (e.Cancelled) console.Text = "User cancelled process";
}
Si
2009-11-30 02:57:00