【IT168技术文档】
这篇短文将演示如何启动与特殊文件关联的应用程序来打开特殊文件,而不必知道关联的应用程序的具体位置和名称。比如,你要打开demo.bmp,通常在 Windows下是MSPaint.exe与之关联的。VB.Net启动关联的应用程序打开特殊文件,需要用到.NetFrameWork System.Diagnostics命名空间。
下面,我们将构造一个任何关联程序的启动器,建一个VB文件Starter.vb。
编译Starter.vbimports 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 test.bmp test.xls
将分别打开MSPaint.exe Execel.exe
附原C#源码:
编译Starter.csusing 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 test.bmp test.xls
将分别打开MSPaint.exe Execel.exe