{"id":6079,"date":"2025-06-07T11:03:46","date_gmt":"2025-06-07T03:03:46","guid":{"rendered":"http:\/\/blog.coolcoding.cn\/?p=6079"},"modified":"2025-06-07T11:04:19","modified_gmt":"2025-06-07T03:04:19","slug":"%e7%94%a8system-diagnostics-process%e7%9a%84readend%e5%8d%a1%e6%ad%bb","status":"publish","type":"post","link":"https:\/\/blog.coolcoding.cn\/?p=6079","title":{"rendered":"\u7528System.Diagnostics.Process\u7684ReadEnd\u5361\u6b7b"},"content":{"rendered":"\n<p>\u4f8b\u5982\u6267\u884cgit pull\u4f1a\u5361\u6b7b<\/p>\n\n\n\n<p>\u7528BeginOutputReadLine+OutputDataReceived \u5c31\u4e0d\u4f1a\u5361\u6b7b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    private static string Execute(string workDir, string cmd, string args, ref string errInfo)\r\n    {\r\n        Console.WriteLine($\"{cmd} {args}\");\r\n\r\n        StringBuilder errstr = new StringBuilder();\r\n        StringBuilder outstr = new StringBuilder();\r\n        var myProcess = new System.Diagnostics.Process();\r\n        try\r\n        {\r\n            myProcess.StartInfo.UseShellExecute = false;\r\n            myProcess.StartInfo.FileName = cmd;\r\n            myProcess.StartInfo.CreateNoWindow = true;\r\n            myProcess.StartInfo.Arguments = args;\r\n            myProcess.StartInfo.RedirectStandardInput = true;\r\n            myProcess.StartInfo.RedirectStandardOutput = true;\r\n            myProcess.StartInfo.RedirectStandardError = true;\r\n            myProcess.StartInfo.StandardOutputEncoding = System.Text.Encoding.UTF8;\r\n            myProcess.StartInfo.StandardErrorEncoding = System.Text.Encoding.UTF8;\r\n            myProcess.Start();\r\n\r\n            myProcess.ErrorDataReceived += (object sender, DataReceivedEventArgs e) =>\r\n            {\r\n                Console.WriteLine(e.Data);\r\n                errstr.AppendLine(e.Data);\r\n            };\r\n\r\n            myProcess.OutputDataReceived += (object sender, DataReceivedEventArgs e) =>\r\n            {\r\n                outstr.AppendLine(e.Data);\r\n            };\r\n\r\n            myProcess.BeginErrorReadLine();\r\n            myProcess.BeginOutputReadLine();\r\n            myProcess.WaitForExit();\r\n            myProcess.Close();\r\n        }\r\n        catch (System.Exception e)\r\n        {\r\n            outstr.AppendLine(e.ToString());\r\n        }\r\n\r\n        errInfo = errstr.ToString();\r\n        return outstr.ToString();\r\n    }\r\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4f8b\u5982\u6267\u884cgit pull\u4f1a\u5361\u6b7b \u7528BeginOutputReadLine+OutputDataReceived [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/6079"}],"collection":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6079"}],"version-history":[{"count":3,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/6079\/revisions"}],"predecessor-version":[{"id":6082,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/6079\/revisions\/6082"}],"wp:attachment":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6079"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6079"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6079"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}