{"id":1296,"date":"2014-02-07T19:51:43","date_gmt":"2014-02-07T19:51:43","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=1296"},"modified":"2014-02-07T19:51:43","modified_gmt":"2014-02-07T19:51:43","slug":"redirecting-standard-output-using-process","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/redirecting-standard-output-using-process\/","title":{"rendered":"Redirecting standard output using Process"},"content":{"rendered":"<p>If we are running a console application, we <em>might<\/em> wish to capture the output for our own use&#8230;<\/p>\n<p><strong>Asynchronous output<\/strong><\/p>\n<p>By default any &#8220;standard&#8221; output from an application run via the Process class, is output to it&#8217;s own window. To capture this output for our own use we can write the following&#8230;<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing (var p = new Process())\r\n{\r\n   p.StartInfo = new ProcessStartInfo(&quot;nant&quot;, &quot;build&quot;)\r\n   {\r\n      UseShellExecute = false,\r\n      RedirectStandardOutput = true\r\n   };\r\n\r\n   p.OutputDataReceived += OnOutputDataReceived;\r\n   p.Start();\r\n   p.BeginOutputReadLine();\r\n   p.WaitForExit();\r\n   return p.ExitCode;\r\n}\r\n<\/pre>\n<p>The above is part of a method that returns an int and runs <em>nant<\/em> with the argument <em>build<\/em>. I wanted to capture the output from this process and direct to my own console window.<\/p>\n<p>The bits to worry about are&#8230;<\/p>\n<ol>\n<li>Set UseShellExecute to false<\/li>\n<li>Set RedirectStandardOutput to true<\/li>\n<li>Subscribe to the OutputDataReceived event<\/li>\n<li>Call BeginOutputReadLine <strong>after<\/strong> you start the process<\/li>\n<\/ol>\n<p><strong>Synchronous output<\/strong><\/p>\n<p>If you want to do a similar thing but capture all the output when the process ends, you can use<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing (var p = new Process())\r\n{\r\n   p.StartInfo = new ProcessStartInfo(&quot;nant&quot;, &quot;build&quot;)\r\n   {\r\n      UseShellExecute = false,\r\n      RedirectStandardOutput = true\r\n   };\r\n\r\n   p.Start();\r\n   p.WaitForExit();\r\n\r\n   using (StreamReader reader = p.StandardOutput)\r\n   {\r\n      Console.WriteLine(reader.ReadToEnd());\r\n   }\r\n   return p.ExitCode;\r\n}\r\n<\/pre>\n<p>In this synchronous version we do not output anything until the process completes, then we read for the process&#8217;s StandardardOutput. We still require <em>UserShellExecute<\/em> and <em>RedirectStandardOutput<\/em> to be setup as previously outlined, but no longer subscribe to any events or use <em>BeginOutputReadLine<\/em> to start the output process.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If we are running a console application, we might wish to capture the output for our own use&#8230; Asynchronous output By default any &#8220;standard&#8221; output from an application run via the Process class, is output to it&#8217;s own window. To capture this output for our own use we can write the following&#8230; using (var p [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1296","post","type-post","status-publish","format-standard","hentry","category-c"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1296","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=1296"}],"version-history":[{"count":6,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1296\/revisions"}],"predecessor-version":[{"id":1304,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1296\/revisions\/1304"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=1296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=1296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=1296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}