{"id":603,"date":"2007-07-16T09:52:18","date_gmt":"2007-07-16T08:52:18","guid":{"rendered":"http:\/\/www.craigmurphy.com\/blog\/?p=603"},"modified":"2007-07-16T09:52:18","modified_gmt":"2007-07-16T08:52:18","slug":"opening-the-net-command-prompt-programatically","status":"publish","type":"post","link":"http:\/\/www.craigmurphy.com\/blog\/?p=603","title":{"rendered":"Opening the .net Command Prompt Programatically"},"content":{"rendered":"<p>In a recent forum posting, I found myself writing some code that would open the Visual Studio 2005 Command Prompt (C:\\Program Files\\Microsoft Visual Studio 8\\VC\\vcvarsall.bat).  Once opened, the command prompt had to accept command-lines, just as if they were typed in by the user&#8230;except in this case it had to be under program control!  <\/p>\n<p>I&#8217;m sure that there are many ways of doing this, but here&#8217;s what I ended up with:<\/p>\n<p>[code lang=&#8221;C#&#8221;]<br \/>\n[C#]<br \/>\nusing System.Diagnostics;<br \/>\nusing System.IO;<\/p>\n<p>namespace Cmd<br \/>\n{<br \/>\n    public partial class Form1 : Form<br \/>\n    {<br \/>\n        public Form1()<br \/>\n        {<br \/>\n            InitializeComponent();<br \/>\n        }<\/p>\n<p>        private void button1_Click(object sender, EventArgs e)<br \/>\n        {<br \/>\n            string sProcess = @&#8221;C:\\windows\\system32\\cmd.exe&#8221;;<br \/>\n            string sParam = @&#8221;C:\\Program Files\\Microsoft Visual Studio 8\\VC\\vcvarsall.bat&#8221;;<br \/>\n            string cmd = String.Format(&#8221; \/k {0}{1}{2} x86&#8243;, &#8220;\\&#8221;&#8221;, sParam, &#8220;\\&#8221;&#8221;);<\/p>\n<p>            Process p = new Process();<br \/>\n            p.StartInfo.RedirectStandardInput = true;<br \/>\n            p.StartInfo.RedirectStandardOutput = true;<br \/>\n            p.StartInfo.UseShellExecute = false;<\/p>\n<p>            p.StartInfo.FileName = sProcess;<br \/>\n            p.StartInfo.Arguments = cmd;<br \/>\n            p.Start();<\/p>\n<p>            System.IO.StreamReader sOut = p.StandardOutput;<br \/>\n            StreamWriter myStreamWriter = p.StandardInput;<\/p>\n<p>            myStreamWriter.WriteLine(&#8220;dir&#8221;);  \/\/ Your command line, MSBuild, etc.<br \/>\n            myStreamWriter.WriteLine(&#8220;EXIT&#8221;);<\/p>\n<p>            MessageBox.Show(sOut.ReadToEnd());<\/p>\n<p>            p.Close();<br \/>\n        }<br \/>\n    }<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>I&#8217;ve left the call to MessageBox.Show() to make you aware of the output.<\/p>\n<p>Technorati Tags: <a href=\"http:\/\/technorati.com\/tag\/.net+command+prompt\" rel=\"tag\">.net command prompt<\/a>, <a href=\"http:\/\/technorati.com\/tag\/C%3A%5CProgram+Files%5CMicrosoft+Visual+Studio+8%5CVC%5Cvcvarsall.bat\" rel=\"tag\">C:\\Program Files\\Microsoft Visual Studio 8\\VC\\vcvarsall.bat<\/a>, <a href=\"http:\/\/technorati.com\/tag\/vcvarsall.bat\" rel=\"tag\">vcvarsall.bat<\/a>, <a href=\"http:\/\/technorati.com\/tag\/RedirectStandardInput\" rel=\"tag\">RedirectStandardInput<\/a>, <a href=\"http:\/\/technorati.com\/tag\/RedirectStandardOutput\" rel=\"tag\">RedirectStandardOutput<\/a>, <a href=\"http:\/\/technorati.com\/tag\/StandardOutput\" rel=\"tag\">StandardOutput<\/a>, <a href=\"http:\/\/technorati.com\/tag\/StandardInput\" rel=\"tag\">StandardInput<\/a>, <a href=\"http:\/\/technorati.com\/tag\/StartInfo\" rel=\"tag\">StartInfo<\/a>, <a href=\"http:\/\/technorati.com\/tag\/Process\" rel=\"tag\">Process<\/a>, <a href=\"http:\/\/technorati.com\/tag\/Visual+Studio+2005+Command+Prompt\" rel=\"tag\">Visual Studio 2005 Command Prompt <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a recent forum posting, I found myself writing some code that would open the Visual Studio 2005 Command Prompt (C:\\Program Files\\Microsoft Visual Studio 8\\VC\\vcvarsall.bat). Once opened, the command prompt had to accept command-lines, just as if they were typed in by the user&#8230;except in this case it had to be under program control! I&#8217;m &hellip; <a href=\"http:\/\/www.craigmurphy.com\/blog\/?p=603\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Opening the .net Command Prompt Programatically<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,3],"tags":[],"class_list":["post-603","post","type-post","status-publish","format-standard","hentry","category-net","category-development"],"_links":{"self":[{"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/603","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=603"}],"version-history":[{"count":0,"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/603\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=603"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}