VB. NET starts external programs

Source: Internet
Author: User
When starting external applications, we usually use Win32 application programming interfaces or vB Shell functions. Now, there is a system. Diagnostics. Process class in the. NET Framework. You will find it much easier to perform this operation through this class.
In traditional vbprograms, you can use shell functions to start an application. When you transfer a data file name, VB opens the data file in the corresponding application. You can use any windowstyle parameter to control the window mode of the started application. For example, in VB6, the following line of code will start the default text editor (usually notepad) and open the file "D:/run.txt ":

Returnid = shell ("D:/run.txt", vbnormalfocus)

Although Microsoft. visualBasic. comaptibility Domain Name Space, in VB. net can still use the shell function, and it has been modified, but in. in the. NET Framework, it is not the best way to start an application, because Shell functions have some strict restrictions, one of which is to start the program asynchronously. After the application is started, your own program will continue to run. Therefore, you cannot use it directly to start a program, and you can only return to your own program after the program exits. To do this in traditional VB, you must turn to the Windows API, which requires you to understand the window handle, process identification number, and enumeration top level window.
Using. Net makes this operation very simple. You can use the process class in the system. Diagnostics Domain Name Space to start external programs. You can simply use the shared process. Start method to start a new process and transmit an executable file name or an extended associated file name of the executable application to it as a parameter. For example, the following code starts the "D:/run.txt" file.

System. Diagnostics. process. Start ("D:/run.txt ")

The start method has an overloaded version and can return a process object. Therefore, you can obtain a reference to the startup process and use it for multiple purposes:

Dim myprocess as process = system. Diagnostics. process. Start
("D:/run.txt ")
MessageBox. Show (myprocess. processname)

At first glance, you seem to have lost the ability to control the window style (remember the second parameter of the shell function ?), However, this is not the case. In many cases, you do not need to explicitly set the window style, because it is started in a normal window with focus (processwindowstyle. Normal) by default. However, if you want to use a different window style, you can use the overloaded process. Start method to receive a processstartinfo object parameter instead of a simple string. To use it, you must first create a processstartinfo object and then set the initial process value. Two overload methods allow you to set a file name, a file name, and a group of command line parameters. The processstartinfo object also has a windowstyle attribute, which consists of the system. Diagnostics. process. windowstyle enumerated values. Therefore, you can call the process. Start method and send a processstartinfo object to control the style of the window to be started.

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 attribute and is a processstartinfo object, another way to produce the same result is to create a process object and set its startinfo attribute. You can only call the start method of a pre-created process object 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
Set process parameters during design

In addition, the. NET Framework comes with a process component that encapsulates the code during design. You can find the component in the toolbar. To use it, drag a process component to your form and expand the startinfo attribute 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.