• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            How can I run another application or batch file from my Visual C# .NET code?

            Published 01 June 04 11:54 PM

            Posted by: Duncan Mackenzie, MSDN
            This post applies to Visual C# .NET 2002/2003

            Suppose you want to run a command line application, open up another Windows program, or even bring up the default web browser or email program... how can you do this from your C# code?

            The answer for all of these examples is the same, you can use the classes and methods in System.Diagnostics.Process to accomplish these tasks and more.

            Example 1. Running a command line application, without concern for the results:

            private void simpleRun_Click(object sender, System.EventArgs e){
             System.Diagnostics.Process.Start(@"C:\listfiles.bat");
            }

            Example 2. Retrieving the results and waiting until the process stops (running the process synchronously):

            private void runSyncAndGetResults_Click(object sender, System.EventArgs e){
             System.Diagnostics.ProcessStartInfo psi =
              
            new System.Diagnostics.ProcessStartInfo(@"C:\listfiles.bat");
             psi.RedirectStandardOutput =
            true;
             psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
             psi.UseShellExecute =
            false;
             System.Diagnostics.Process listFiles;
             listFiles = System.Diagnostics.Process.Start(psi);
             System.IO.StreamReader myOutput = listFiles.StandardOutput;
             listFiles.WaitForExit(2000);
             
            if (listFiles.HasExited)
             {
              
            string output = myOutput.ReadToEnd();
              
            this.processResults.Text = output;
             }
            }

             Example 3. Displaying a URL using the default browser on the user's machine:

            private void launchURL_Click(object sender, System.EventArgs e){
             
            string targetURL = @http://www.duncanmackenzie.net;
             System.Diagnostics.Process.Start(targetURL);
            }

            In my opinion, you are much better off following the third example for URLs, as opposed to executing IE with the URL as an argument. The code shown for Example 3 will launch the user's default browser, which may or may not be IE; you are more likely to provide the user with the experience they want and you will be taking advantage of the browser that is most likely to have up-to-date connection information.

            C# code download available from http://www.duncanmackenzie.net/Samples/default.aspx

            By the way, this post is a simple port of an earlier VB FAQ post... just in case you thought you had seen it already in your feeds...

            Posted on 2009-11-17 13:10 micheal's tech 閱讀(311) 評論(0)  編輯 收藏 引用

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            久久精品国产久精国产果冻传媒| 久久午夜无码鲁丝片| 国产99久久九九精品无码| 麻豆精品久久久一区二区| 久久亚洲国产成人影院网站| 伊人久久大香线蕉成人| 99久久婷婷免费国产综合精品| 欧美国产精品久久高清| 国产精品美女久久久久久2018| 日韩十八禁一区二区久久| 久久精品国产亚洲AV麻豆网站| 亚洲国产一成久久精品国产成人综合| 新狼窝色AV性久久久久久| 人妻系列无码专区久久五月天| 国产亚洲综合久久系列| 久久99国产精品久久99小说| 亚洲国产精品一区二区久久| 久久久久久久人妻无码中文字幕爆| 久久se这里只有精品| 秋霞久久国产精品电影院| 精品熟女少妇av免费久久| 久久久久久久波多野结衣高潮| 久久久久久毛片免费看| 久久国产乱子伦精品免费强| 久久久久亚洲AV无码专区体验| 奇米影视7777久久精品人人爽| 四虎国产精品成人免费久久| 久久精品成人免费国产片小草| 久久99国产精品二区不卡| 亚洲AV无码久久精品色欲| 一本久久综合亚洲鲁鲁五月天| 国产精品嫩草影院久久| 久久亚洲欧美日本精品| 成人资源影音先锋久久资源网| 亚洲精品美女久久久久99| 77777亚洲午夜久久多人| 亚洲中文字幕无码久久精品1 | 午夜精品久久久久久毛片| 国产亚洲美女精品久久久2020| 欧美伊人久久大香线蕉综合69 | 久久综合久久综合亚洲|