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?
相关问题
- How to get the return code of a shell script in lu
- Generic Generics in Managed C++
- How to Debug/Register a Permanent WMI Event Which
- 'System.Threading.ThreadAbortException' in
- Bulk update SQL Server C#
Another way would be to use P/Invoke and use ShellExecute directly:
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:
There is an easy way to handle this in C#. Using the System.Diagnostics namespace, there is a class to handle spawning processes.
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.