bin*_*bin 7 .net c# asp.net .net-core asp.net-core
我在linux上有一个asp.net核心web应用程序,现在,我想执行shell命令并获取结果表单命令,我不知道,请帮助我.
有没有办法从ASP.NET Core应用程序中执行linux shell命令并将值返回到变量?
string RunCommand(string command, string args)
{
var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = command,
Arguments = args,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
}
};
process.Start();
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
process.WaitForExit();
if (string.IsNullOrEmpty(error)) { return output; }
else { return error; }
}
// ...
string rez = RunCommand("date", string.Empty);
Run Code Online (Sandbox Code Playgroud)
我还会添加一些方法来判断返回的字符串是错误还是只是“正常”输出(作为消息返回Tuple<bool, string>或抛出异常error)。
| 归档时间: |
|
| 查看次数: |
2422 次 |
| 最近记录: |