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?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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);
回答2:
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, Windows Forms, 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 the constructor 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";
}
回答3:
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.