Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How VB.NET uses ProcessStartInfo objects

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "VB.NET how to use ProcessStartInfo object", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "VB.NET how to use ProcessStartInfo object" this article.

When starting external applications, we usually use the Win32 API or the Shell function of VB to solve the problem. Now, there is a System.Diagnostics.Process class in the .NET Framework, and you will find it much easier to do this through this class.

In traditional VB programs, you can use the Shell function to start an application. When you transfer a data file name, VB opens the data file in the corresponding application. You can use an optional windowstyle parameter to control the window mode of the launched application. For example, in VB6, the following line starts the default text editor (usually notepad) and opens the file "D:\ run.txt":

ReturnID = Shell ("D:\ run.txt", vbNormalFocus)

Although the Shell function can still be used in VB.NET through the Microsoft.VisualBasic.Comaptibility domain name space, and it has been changed, it is not a way to start an application in the .NET Framework, because the Shell function has some strict restrictions, one of which is that you can only start the program asynchronously; after you start the application, your own program continues to run. So you can't use it directly to start a program, and you can't return to your own program until the program exits. To do this in traditional VB, you have to turn to Windows API, which requires knowledge of window handles, process identifiers, enumerated windows, and so on.

Using .NET, you can make this operation very simple. You can use the Process class in the System.Diagnostics domain space to start external programs. You can simply use the shared Process.Start method to start a new process, passing an executable file name or an extended associated file name of the executable application 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 that returns a Process object, so you can get a reference to the startup process and use it for a variety of purposes:

Dim myProcess As Process = System.Diagnostics.Process.Start ("D:\ run.txt") MessageBox.Show (myProcess.ProcessName)

At first glance, it looks as if you have lost the ability to control the window style (remember the second argument to the Shell function? ), but this 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 (ProcessWindowStyle.Normal) with focus. But if you want to use a different window style, you can use the overloaded Process.Start method to receive a VB.NET ProcessStartInfo object parameter instead of a simple string. To use it, first create a VB.NET ProcessStartInfo object, and then set the process initial value. Two overloading methods let you set a file name or a file name and a set of command line arguments. And the VB.NET 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 pass a VB.NET ProcessStartInfo object to control the style of the launched 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 VB.NET ProcessStartInfo object, another way to produce the same result is to create a Process object and set its StartInfo property. When you pre-create a Process object, you can just call its Start method instead of 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

In addition, the .NET Framework is shipped with Process components that encapsulate this code at design time. You can find it in the Components section of the toolbar. To use it, drag a Process component onto your form and expand the StartInfo property in the properties window.

These are all the contents of the article "how VB.NET uses ProcessStartInfo objects". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report