Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Launching and Monitoring External Programs from VB.NET Applications

You no longer need to use the Win32 API or the VB Shell function to launch external applications. Instead, use the System.Diagnostics.Process class built into the .NET framework to simplify your code.


advertisement
lthough .NET makes some things more complicated, launching external programs is not one of them. In classic VB, you used the Shell function to launch an application when you passed an executable file name. When you passed a data file name, VB opened the data file in its associated application. You could also control the window style of the launched application with an optional windowstyle parameter. For example, in VB6, the following line would launch the default text editor (usually NotePad) and open the file "c:\somepath\somefile.txt":

returnID = Shell("c:\somepath\somefile.txt", _ vbNormalFocus)

The Shell function still exists in VB.NET through the Microsoft.VisualBasic.Comaptibility namespace, and it has been improved a bit, but it's often not the best way to launch programs in the .NET framework. In classic VB, the Shell Function had some serious limitations, one of which was that it launched programs asynchronously; after launching a program, your program code continues to run. So you couldn't use it directly to launch a program and wait for that program to exit before returning to the task of processing code in your own program. Instead, you had to fall back on the Windows API, which required an understanding of window handles, process IDs, enumerating top-level windows, and so on. For a more complete explanation, see the Microsoft Knowledge Base topics "Q96844: HOWTO: Determine When a Shelled Process Has Terminated (16-Bit)" and "Q129796 - HOWTO: 32-Bit App Can Determine When a Shelled Process Ends."

With .NET things have beome much simpler. The System.Diagnostics namespace exposes a Process class that you can use to launch external programs. At the simplest level, you can launch a new process with the shared Process.Start method, passing it either the name of an executable file or a filename with an extension associated with an executable application. For example, the following code launches the "c:\somepath\somefile.txt" file:

System.Diagnostics.Process.Start _ ("c:\somepath\somefile.txt")

The Start method has an overloaded version that returns a Process object, so you can obtain a reference to the launched process, and use it for various purposes:



Dim myProcess As Process = System.Diagnostics.Process.Start ("c:\somepath\somefile.txt") MessageBox.Show(myProcess.ProcessName)

At first glance, you seem to have lost the ability to control the window style (remember the second parameter from the Shell function?), but you haven't. In many cases, you won't need to set the window style explicitly, because the default is to display the launched process in a normal window with the focus (ProcessWindowStyle.Normal). But when you want to use a different window style, an overloaded Process.Start method accepts a ProcessStartInfo object parameter rather than a simple string. To use it, first create a ProcessStartInfo object and then set process initialization values. Two overloaded methods let you set a filename or a filename and a set of command-line parameters. And the ProcessStartInfo object also has a WindowStyle property, which consists of values from the System.Diagnostics.Process.WindowStyle enumeration. So you can call the Process.Start method and pass a ProcessStartInfo object to control the launched window's style.

Dim psInfo As New _ System.Diagnostics.ProcessStartInfo _ ("c:\somepath\somefile.txt") psInfo.WindowStyle = _ System.Diagnostics.ProcessWindowStyle.Normal Dim myProcess As Process = _ System.Diagnostics.Process.Start(psInfo)

Because the Process class exposes a StartInfo property that's a ProcessStartInfo object, another way to accomplish the same result is to create a Process object and set its StartInfo properties. When you use a pre-created Process object, you can simply call its Start method rather than the Process class's shared Start method.

Dim myProcess As System.Diagnostics.Process = _ new System.Diagnostics.Process() myProcess.StartInfo.FileName = _ "c:\somepath\somefile.txt" myProcess.StartInfo.WindowStyle = _ System.Diagnostics.ProcessWindowStyle.Normal myProcess.Start

Setting Process Parameters at Design Time
The .NET framework ships with a Process component that encapsulates all this code at design time. You can find it in the Components area of the Toolbar. To use it, drag a Process component onto your form and then expand the StartInfo property in the Properties window, and set the StartInfo values to your liking (see Figure 1).

Figure 1: You can add a Process component to a form that lets you set properties at design time rather than at run time.


Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap