• <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 閱讀(303) 評論(0)  編輯 收藏 引用
            久久久久精品国产亚洲AV无码| 欧美日韩中文字幕久久久不卡| 一级A毛片免费观看久久精品| 久久精品国产亚洲αv忘忧草| 97久久国产露脸精品国产| 97久久精品无码一区二区天美| 久久www免费人成看国产片| 精品国产乱码久久久久久郑州公司| 久久综合亚洲色HEZYO国产| 94久久国产乱子伦精品免费| 亚洲伊人久久精品影院| 久久国产成人精品国产成人亚洲| 久久亚洲精品成人无码网站| 亚洲&#228;v永久无码精品天堂久久| 日本亚洲色大成网站WWW久久| 久久夜色精品国产噜噜亚洲AV| 日韩欧美亚洲综合久久| 久久99国产精品久久99果冻传媒| 久久99精品综合国产首页| 午夜精品久久久久久久无码| 精品久久久久久久久午夜福利| 久久乐国产综合亚洲精品| 精品国产乱码久久久久软件| 亚洲伊人久久大香线蕉苏妲己| 99久久夜色精品国产网站| 精品久久人人做人人爽综合| 国产精品视频久久| 欧美黑人又粗又大久久久| 亚洲国产成人久久一区久久| 久久精品成人一区二区三区| 香蕉久久夜色精品国产尤物| 国产精品gz久久久| 久久亚洲精品国产亚洲老地址| 国产精品久久久久乳精品爆| 94久久国产乱子伦精品免费| 中文精品久久久久国产网址| 日韩精品国产自在久久现线拍 | 综合网日日天干夜夜久久| 久久精品国产亚洲5555| 亚洲AV无码久久精品成人| 91精品国产综合久久婷婷|