gpt4 book ai didi

c# - 从 ASP.NET Core 3 应用程序执行 Linux Shell 命令

转载 作者:行者123 更新时间:2023-12-02 02:40:03 26 4
gpt4 key购买 nike

我的问题字面意思是,如何从我的应用程序执行 Shell 命令。还有类似的Post ,但这显示了如何执行脚本文件并需要该文件的路径。

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);

关于c# - 从 ASP.NET Core 3 应用程序执行 Linux Shell 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63769059/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com