Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute a shell command from a .NET application

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?

like image 208
RCIX Avatar asked Nov 30 '09 02:11

RCIX


People also ask

How do I run a shell script in Visual Studio?

In this case it's simple : add your . sh script file to your Visual Studio solution one way or another, then right-click on it in the Solution Explorer, and select « Open With… ». This will determine what program gets called when you double click on the file in this view.

What is Shell in C#?

Shell Sort allows the exchange of items that are far apart in the array and then reduces the gap between them. This is a sort of generalization of Insertion Sort. Shell Sort is known as such as it was published by Donald Shell at first. A program that demonstrates shell sort in C# is given as follows −


2 Answers

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);
like image 149
data Avatar answered Sep 28 '22 11:09

data


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";
}
like image 42
si618 Avatar answered Sep 28 '22 12:09

si618