[轉貼] .NET 中運行外部程序的3種方法

 

在win32中有ShellExecute方法可以使我們啟動外部的應用程序,在 .NET FrameWork 中我們可以使用Process類來完成類似的功能。
Process在System.Diagnostics中,所以別忘了:
    using System.Diagnostics;


1) 用Process的靜態方法Start


//啟動記事本
Process.Start("notepad.exe");
//啟動記事本,並打開temp.txt文件
        Process.Start("notepad.exe",@"d:\temp.txt");
    此方法最簡單,但功能有限


2) 用帶有ProcessStartInfo參數的 Start方法


             ProcessStartInfo startInfo = new ProcessStartInfo("notepad.exe");
             startInfo.Arguments=@" d:\temp.txt ";
//啟動時最小化
             startInfo.WindowStyle = ProcessWindowStyle.Minimized;
             startInfo.Verb="open";
         Process.Start(startInfo);


3)實例化Process類


             Process process=new Process();
             process.StartInfo.FileName="notepad.exe";
             process.StartInfo.Verb="open";
process.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
             process.StartInfo.Arguments=@" d:\temp.txt";
         process.Start();


第2種方法和第3種方法差不多,他們的可選的功能就比較多了。

留言

這個網誌中的熱門文章

TeknoParrot 模擬器介紹,俗稱《鸚鵡模擬器》

[轉貼] ASP.NET傳遞參數給Crystal Report做Export或Print的動作

[轉貼] Server 端Post資料到 .ashx