Execute a shell command from a .NET application

2019-04-08 22:54发布

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?

3条回答
【Aperson】
2楼-- · 2019-04-08 22:59
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);
查看更多
ら.Afraid
3楼-- · 2019-04-08 23:09

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";
}
查看更多
smile是对你的礼貌
4楼-- · 2019-04-08 23:18

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.

查看更多
登录 后发表回答