{"id":864,"date":"2009-10-13T16:03:37","date_gmt":"2009-10-13T14:03:37","guid":{"rendered":"http:\/\/www.navision-blog.de\/2009\/10\/13\/redirecting-process-output-in-f\/"},"modified":"2009-10-13T16:05:07","modified_gmt":"2009-10-13T14:05:07","slug":"redirecting-process-output-in-f","status":"publish","type":"post","link":"http:\/\/www.navision-blog.de\/blog\/2009\/10\/13\/redirecting-process-output-in-f\/","title":{"rendered":"Redirecting process output in F#"},"content":{"rendered":"<p>Today I had some trouble to redirect the default and error output from a created process. So here is my final solution:<\/p>\n<div style=\"font-family: courier new; background: white; color: black; font-size: 10pt\">\n<p style=\"margin: 0px\"><span style=\"color: blue\">&#160; open<\/span> System.Diagnostics<\/p>\n<p style=\"margin: 0px\"><span style=\"color: blue\">&#160; open<\/span> System.Threading<\/p>\n<p style=\"margin: 0px\">&#160;<\/p>\n<\/p><\/div>\n<div style=\"font-family: courier new; background: white; color: black; font-size: 10pt\">\n<p style=\"margin: 0px\">&#160; <span style=\"color: green\">\/\/ outputF: string -&gt; unit<\/span><\/p>\n<p style=\"margin: 0px\">&#160; <span style=\"color: green\">\/\/ errorF: string -&gt; unit<\/span><\/p>\n<p style=\"margin: 0px\">&#160; <span style=\"color: blue\">let<\/span> runProcess outputF errorF = <\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160; <span style=\"color: blue\">use<\/span> p = <span style=\"color: blue\">new<\/span> Process()<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160; p.StartInfo.UseShellExecute &lt;- <span style=\"color: blue\">false<\/span><\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160; <span style=\"color: green\">\/\/ &#8230;<\/span><\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160; p.StartInfo.RedirectStandardOutput &lt;- <span style=\"color: blue\">true<\/span><\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160; p.StartInfo.RedirectStandardError &lt;- <span style=\"color: blue\">true<\/span><\/p>\n<p style=\"margin: 0px\">&#160;<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160; p.ErrorDataReceived.Add <\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160; (<span style=\"color: blue\">fun<\/span> d \u2013<span style=\"color: blue\">&gt;<\/span> <span style=\"color: blue\">if<\/span>&#160; d.Data &lt;&gt; <span style=\"color: blue\">null<\/span> <span style=\"color: blue\">then<\/span> errorF d.Data)<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160; p.OutputDataReceived.Add <\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160; (<span style=\"color: blue\">fun<\/span> d \u2013<span style=\"color: blue\">&gt;<\/span> <span style=\"color: blue\">if<\/span> d.Data &lt;&gt; <span style=\"color: blue\">null<\/span> <span style=\"color: blue\">then<\/span> outputF d.Data)<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160; p.Start() |&gt; ignore<\/p>\n<p style=\"margin: 0px\">&#160;<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160; p.BeginErrorReadLine()<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160; p.BeginOutputReadLine()&#160;&#160;&#160;&#160; <\/p>\n<p style=\"margin: 0px\">&#160;<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160; p.WaitForExit()<\/p>\n<p style=\"margin: 0px\">&#160;<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160; p.ExitCode<\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Today I had some trouble to redirect the default and error output from a created process. So here is my final solution: &#160; open System.Diagnostics &#160; open System.Threading &#160; &#160; \/\/ outputF: string -&gt; unit &#160; \/\/ errorF: string -&gt; unit &#160; let runProcess outputF errorF = &#160;&#160;&#160; use p = new Process() &#160;&#160;&#160; p.StartInfo.UseShellExecute [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[448],"tags":[664,568],"_links":{"self":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/864"}],"collection":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/comments?post=864"}],"version-history":[{"count":1,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/864\/revisions"}],"predecessor-version":[{"id":865,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/864\/revisions\/865"}],"wp:attachment":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/media?parent=864"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/categories?post=864"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/tags?post=864"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}