我正在使用dotnet-core 1.1.centos bash
以何种方式运行grep或wget并检索结果?
像Windows中的cmd,但我需要grep实时日志文件
System.Diagnostics.Process.Start( "Notepad.exe的")
小智 6
您可以启动一个过程来grep并检索结果,您可以参考以下代码。
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();
Run Code Online (Sandbox Code Playgroud)
我相信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
Run Code Online (Sandbox Code Playgroud)
无耻的插件,我还制作了一个包,可以轻松抽象在 mac/win/linux 上打开东西,基本上抽象 xdg-open (ubuntu)、open (mac)、cmd.exe (win),所以你不必考虑它
https://github.com/TerribleDev/Opener.Net