Abdeali Siyawala
Abdeali Siyawala

Reputation: 80

How do I execute a shell script in C#?

I have one file which contains a Unix shell script. So now I wanted to run the same in .NET. But I am unable to execute the same.

So my point is, is it possible to run the Unix program in .NET? Is there any API like NSTask in Objective-C for running Unix shell scripts so any similar API in .NET?

Upvotes: 6

Views: 17127

Answers (2)

Tejas Katakdhond
Tejas Katakdhond

Reputation: 313

        ProcessStartInfo frCreationInf = new ProcessStartInfo();
        frCreationInf.FileName = @"C:\Program Files\Git\git-bash.exe";
        frCreationInf.Arguments = "Test.sh";
        frCreationInf.UseShellExecute = false;
        var process = new Process(); 
        process.StartInfo = frCreationInf;
        process.Start();
        process.WaitForExit();

Upvotes: 0

Pouya Samie
Pouya Samie

Reputation: 3723

It has been answered before. Just check this out.

By the way, you can use:

Process proc = new Process {
    StartInfo = new ProcessStartInfo {
        FileName = "program.exe",
        Arguments = "command line arguments to your executable",
        UseShellExecute = false,
        RedirectStandardOutput = true,
        CreateNoWindow = true
    }
};

After that start the process and read from it:

proc.Start();
while (!proc.StandardOutput.EndOfStream) {
    string line = proc.StandardOutput.ReadLine();
    // Do something with line
}

Upvotes: 8

Related Questions