• <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 閱讀(306) 評論(0)  編輯 收藏 引用
            久久久久亚洲AV成人网人人网站| 国内精品久久久久久久久电影网 | 久久综合九色综合网站| 2021国内精品久久久久久影院| 久久综合亚洲色一区二区三区| 久久精品国产亚洲av影院| 99久久国产综合精品五月天喷水| 欧美粉嫩小泬久久久久久久 | 性做久久久久久久久| 久久夜色精品国产网站| 久久久久久亚洲精品无码| 精品少妇人妻av无码久久| 久久久久亚洲AV无码专区首JN| 99久久国产综合精品成人影院 | 久久人人爽爽爽人久久久| 91久久精品无码一区二区毛片| 久久人人爽人人人人爽AV | 久久亚洲sm情趣捆绑调教| 久久九九亚洲精品| 97精品依人久久久大香线蕉97| 国产一区二区三区久久精品| 无码人妻久久一区二区三区免费丨 | 午夜精品久久久内射近拍高清| 国内精品久久久久影院优| 中文成人无码精品久久久不卡 | 久久亚洲视频| 99久久精品免费看国产| 久久夜色精品国产亚洲| 久久男人Av资源网站无码软件| 午夜精品久久久久久久无码| 久久精品国产一区二区三区不卡 | 波多野结衣久久一区二区 | 久久国产乱子伦免费精品| 亚洲乱码日产精品a级毛片久久| 91精品婷婷国产综合久久| 精品国产福利久久久| 精品久久久无码人妻中文字幕豆芽 | 久久亚洲国产成人精品性色| 国产成人无码精品久久久性色| 久久无码高潮喷水| 色欲av伊人久久大香线蕉影院|