• <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 閱讀(305) 評論(0)  編輯 收藏 引用
            国产精品久久久久久久久久免费| 99久久精品久久久久久清纯| 久久久网中文字幕| 久久一区二区三区99| 久久久久久精品免费免费自慰| 精品久久亚洲中文无码| 久久无码人妻一区二区三区| 久久久久亚洲av毛片大| 中文字幕久久精品无码| 伊人色综合久久天天网| 国产精品九九久久精品女同亚洲欧美日韩综合区 | 国产精品久久久福利| 精品久久综合1区2区3区激情| 久久精品中文无码资源站| 国产精品久久久久久五月尺| 狠狠人妻久久久久久综合蜜桃| 国内精品伊人久久久久妇| 中文字幕成人精品久久不卡| 伊人久久大香线蕉AV色婷婷色| 久久精品免费大片国产大片| 久久综合狠狠综合久久激情 | 久久久国产精华液| 亚洲国产成人久久精品99 | 精品国产综合区久久久久久| 久久亚洲国产精品成人AV秋霞| 国内精品免费久久影院| 久久精品国产精品青草app| 久久精品9988| 久久99国产乱子伦精品免费| 久久亚洲AV成人无码电影| 久久香综合精品久久伊人| 亚洲欧美国产精品专区久久| 久久精品国产亚洲Aⅴ香蕉| 久久综合久久综合九色| 色综合久久88色综合天天| 69SEX久久精品国产麻豆| 久久精品国产亚洲AV无码麻豆 | 久久亚洲精品无码观看不卡| 国产精品九九久久精品女同亚洲欧美日韩综合区 | 久久综合久久自在自线精品自| 久久人人爽人人爽人人AV|