我有一个包含Unix shell脚本的文件。现在我想在.NET中运行相同的脚本,但是我无法执行它。
我的问题是,是否可能在.NET中运行Unix程序?是否有类似于Objective-C中NSTask的API来运行Unix shell脚本,那么.NET中是否有类似的API呢?
这个问题之前已经有人回答过了,你可以查看这里。
另外,你可以使用以下方法:
Process proc = new Process {
StartInfo = new ProcessStartInfo {
FileName = "program.exe",
Arguments = "command line arguments to your executable",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
然后启动该进程并从中读取:
proc.Start();
while (!proc.StandardOutput.EndOfStream) {
string line = proc.StandardOutput.ReadLine();
// Do something with line
}
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();