Vb. NET start external Program

Source: Internet
Author: User
When a program launches an external application, we usually use the WIN32 application programming interface or VB Shell function to solve it. Now, there's a System.Diagnostics.Process class in the. NET Framework that you can use to do this, and you'll find it much easier.
In traditional VB programs, you can use the Shell function to start an application. When you send a data file name, VB opens the data file in the appropriate application. You can use an optional windowstyle parameter to control the window mode of the application that is started. For example, in VB6, the following line of code launches the default text editor (usually Notepad) and opens the file "D:\run.txt":

Returnid = Shell ("D:\run.txt", Vbnormalfocus)

Although the shell feature is still available in vb.net through the Microsoft.VisualBasic.Comaptibility domain name space, and it has been changed, it is not the best way to start an application in the. NET Framework because the Shell function has some strict limitations System, one of which is to start the program asynchronously, and your own program to continue running after you start the application. So you can't just use it to start a program, and you can only wait until the program exits before you can return to your own program. To do this in traditional VB, you have to turn to the Windows API, which requires an understanding of window handles, process identification numbers, and enumeration of top-level windows.
With. NET, you can make this operation simple. You can use the Process class in the System.Diagnostics domain namespace to start an external program. You can simply use the shared Process.Start method to launch a new process that transmits an executable filename or an extended associated filename of an executable application as a parameter to it. For example, the following code launches the "D:\run.txt" file.

System.Diagnostics.Process.Start ("D:\run.txt")

The Start method has an overloaded version that returns a Process object, so you can get a reference to the startup process and can be used for a variety of purposes:

Dim myprocess as Process = System.Diagnostics.Process.Start
("D:\run.txt")
MessageBox.Show (Myprocess.processname)

At first glance, you seem to lose the ability to control the style of the window (remember the second argument of the Shell function?). ), but that is not the case. In many cases, you don't need to explicitly set the window style, because the default is to start the process in a normal window with focus (Processwindowstyle.normal). But if you want to use a different window style, you can use an overloaded Process.Start method to receive a ProcessStartInfo object argument instead of a simple string. To use it, you first create a ProcessStartInfo object, and then set the process initial value. Two overload methods let you set a filename or a filename and a set of command-line arguments. And the ProcessStartInfo object also has a WindowStyle property, which consists of the values of the System.Diagnostics.Process.WindowStyle enumeration. So you can call the Process.Start method and send a ProcessStartInfo object to control the style of the startup window.

Dim PsInfo as New _
System.Diagnostics.ProcessStartInfo _
("D:\run.txt")
Psinfo.windowstyle = _
System.Diagnostics.ProcessWindowStyle.Normal
Dim myprocess as Process = _
System.Diagnostics.Process.Start (PsInfo)

Because the process class has a StartInfo property, which is a ProcessStartInfo object, another way to produce the same result is to create a process object and set its StartInfo property. When you create a process object in advance, you can simply call its start method without using the shared Start method of the Process class.

Dim myprocess as System.Diagnostics.Process = _
New System.Diagnostics.Process ()
MyProcess.StartInfo.FileName = _
"D:\run.txt"
MyProcess.StartInfo.WindowStyle = _
System.Diagnostics.ProcessWindowStyle.Normal
Myprocess.start
To set process parameters during design time

Other than that. NET Framework was shipped with a process component that encapsulates the code during design time. You can find it in the Components section of the toolbar. To use it, drag a process component onto your form, and then expand the StartInfo property in the Properties window.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.