C# 从ASP.NET Core 3应用程序执行Linux Shell命令
我的问题是,如何从应用程序执行Shell命令。有一个类似的示例,但它显示了如何执行脚本文件,并且需要该文件的路径C# 从ASP.NET Core 3应用程序执行Linux Shell命令,c#,linux,asp.net-mvc,shell,asp.net-core,C#,Linux,Asp.net Mvc,Shell,Asp.net Core,我的问题是,如何从应用程序执行Shell命令。有一个类似的示例,但它显示了如何执行脚本文件,并且需要该文件的路径 var process = new Process() { StartInfo = new ProcessStartInfo { FileName = command, // Path required here Arguments = args, RedirectStan
var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = command, // Path required here
Arguments = args,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
}
};
process.Start();
为什么要将命令作为字符串传递?
因为我想插值它。否则,我将不得不创建一个带有一些输入参数的脚本。因为我不太擅长使用Shell,所以我更喜欢简单的方法。假设您想在bash中运行
echo hello
Process process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "bash",
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false
}
};
process.Start();
await process.StandardInput.WriteLineAsync("echo hello");
var output = await process.StandardOutput.ReadLineAsync();
Console.WriteLine(output);