我正在使用 dotnet-core 1.1,CentOS Bash。有没有一种方法可以运行 grep 或 wget 并检索结果?类似于 Windows 中的 cmd,但我需要实时查看日志文件,像这样:
System.Diagnostics.Process.Start("notepad.exe")
System.Diagnostics.Process.Start("notepad.exe")
System.Diagnostics.ProcessStartInfo procStartInfo;
procStartInfo = new System.Diagnostics.ProcessStartInfo("/bin/bash", "-c \"cat myfile.log | grep -a 'dump f'\"");
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardError = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
// Get the output into a string
result = proc.StandardOutput.ReadToEnd();
我相信在System.Diagnostics.Process nuget包中的 System.Diagnostics.Process.Start(..) 方法可以将 ProcessStartInfo 类型作为其中一个重载。当该类型设置为 true 时,以下属性将重定向日志到由 System.Diagnostics.Process 返回的 Process 类型中的流中:
var proc = System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo() {
RedirectStandardOutput = true,
RedirectStandardInput = true,
RedirectStandardError = true
} );
proc.StandardError //stream with stderror
proc.StandardInput //stream with stdin
proc.StandardOutput //stream with stdout