啟動關聯的應用程序打開特殊文件zlyperson(原作)
原文是C#,我把它改成了VB.但未來得及測試.若發現什么問題請告之.
這篇短文將演示如何啟動與特殊文件關聯的應用程序來打開特殊文件,而不必知道關聯的應用程序的具體位置和名稱。比如,你要打開demo.bmp,通常在Windows下是MSPaint.exe與之關聯的。VB.Net啟動關聯的應用程序打開特殊文件,需要用到.NetFrameWork System.Diagnostics命名空間。 下面,我們將構造一個任何關聯程序的啟動器,建一個VB文件Starter.vb。 imports System; imports System.IO; imports System.Diagnostics;
public class Starter public shared sub new(args as string()) '首先,建立進程啟動信息的結構 dim pInfo as new ProcessStartInfo(); pInfo.UseShellExecute = true; dim i as integer for i = 0 to args.Length-1 if File.Exists(args[i]) then pInfo.FileName = args[i]; '啟動進程 dim p as Process = Process.Start(pInfo); end if next end sub end class 編譯Starter.vb 執行Starter test.bmp test.xls 將分別打開MSPaint.exe Execel.exe 文中瑕弊聯系zlyperson@163.net
附原C#源碼:
using System; using System.IO; using System.Diagnostics;
public class Starter { public static void Main(string[] args) { //首先,建立進程啟動信息的結構 ProcessStartInfo pInfo = new ProcessStartInfo(); pInfo.UseShellExecute = true; for ( int i = 0; i < args.Length; i++ ) { if (File.Exists(args[i])) { pInfo.FileName = args[i]; //啟動進程 Process p = Process.Start(pInfo); } } } } 編譯Starter.cs 執行Starter test.bmp test.xls 將分別打開MSPaint.exe Execel.exe
|