Process.start五种用法

 
Process.Start命令行的5种使用方法
1. Process.Start 实例

First, here is an example VB.NET program that uses Process.Start to open the file manager on your C:\ drive. When you run this example the root directory folder will open.
Module Module1
Sub Main()
Process.Start("C:\")
End Sub
End Module

Description of the example code. The Main entry point is declared above and it contains a single line of code, which calls the Process.Start Shared method. It passes one parameter to Process.Start, the directory root.
2. Process.Start打开TXT文本文件

Here we see that when you specify a certain file for Process.Start to open, the default Windows file viewer for the file type will open. This is useful for text files, Microsoft Office files, and many other files.
Module Module1
Sub Main() 'vb.net源码和实例,来自乐博网 www.lob.cn
' Open the file 'example.txt' at the current program's directory.
' It will appear in the default text file viewer.
Process.Start("example.txt")
End Sub
End Module

Description of the example VB.NET code. The Main entry point subroutine is declared above. It contains a comment and one call to Process.Start. The "example.txt" file must be found in the program's current directory. Notepad or any text viewer will open. This can be useful for "Read Me" files.

Description of file paths. Instead of just passing "example.txt", you could pass "C:\Users\Sam\example.txt" to specify the absolute directory.
3. Process.Start进行google搜索

In many programs, resources can be given to the user in the form of URLs. You can tell Windows to launch a web browser window with a specific URL. All you have to do is send Process.Start the URL. It is important that you do not specify IEXPLORE unless IE is required.
Module Module1
Sub Main()
SearchGoogle("乐博网")
End Sub

''' <summary>
''' Open the user's default browser and search for the parameter.
''' </summary>
Private Sub SearchGoogle(ByVal t As String)
Process.Start("http://www.google.cn/search?q=" & t)
End Sub
End Module

Description of the example subroutines. You can see that the subroutine SearchGoogle() is called with a String parameter. The SearchGoogle function is then run, and it calls Process.Start. The base URL for a Google search is specified. The query string is then set to the parameter.


Note on specifying browser executables. Many users and organizations have certain web browsers they use. However, these should be set at the system-level in Windows. Therefore, you shouldn't normally specify IEXPLORE.EXE here.

4. Process.Start打开微软Word

Many applications need to launch Word documents for editing or viewing. It is best to start Word in an external process for the greatest simplicity of code. The example below is similar to the previous one, but it uses a custom FileName field and Arguments.
Module Module1
Sub Main()
OpenMicrosoftWord("C:\Users\Sam\Documents\Office\Gears.docx")
End Sub

''' <summary>
''' Open the path parameter with Microsoft Word.
''' </summary>
Private Sub OpenMicrosoftWord(ByVal f As String)
Dim startInfo As New ProcessStartInfo
startInfo.FileName = "WINWORD.EXE"
startInfo.Arguments = f
Process.Start(startInfo)
End Sub
End Module

Description of the example code. You can see that there are two methods, the first being the Main entry point. The OpenMicrosoftWord subroutine accepts one String ByVal.

Description of OpenMicrosoftWord sub. This part of the code assigns WINWORD.EXE as the FileName, and then the path of the DOCX file as the Arguments. The result is that Microsoft Word opens the file.

5. VB.NET Process.Start运行可执行程序

Here we see how you can utilize a more complex Arguments String and also assign the WindowStyle for a command-line program. The command line program shown here is located at "C:\7za.exe", which is a command-line compression utility.
Module Module1
Sub Main() 'vb.net源码和实例,来自乐博网 www.lob.cn
' One file parameter to the executable
Dim sourceName As String = "ExampleText.txt"
' The second file parameter to the executable
Dim targetName As String = "Example.gz"

' New ProcessStartInfo created
Dim p As New ProcessStartInfo

' Specify the location of the binary
p.FileName = "C:\7za.exe"

' Use these arguments for the process
p.Arguments = "a -tgzip """ & targetName & """ """ & sourceName & """ -mx=9"

' Use a hidden window
p.WindowStyle = ProcessWindowStyle.Hidden

' Start the process
Process.Start(p)
End Sub
End Module

Description of the example code. First, the Main subroutine entry point is declared and executed. Two variable Strings are declared. The two strings indicate two argument parameters we want to use with the executable.

Description of ProcessStartInfo. ProcessStartInfo is another class that encapsulates several fields needed for telling .NET how to start the FileName program. We specify FileName, Arguments, and WindowStyle.

Description of Arguments. The arguments String here is composed of 5 Strings concatenated together with the & operator. It is useful to break in the debugger to verify your Arguments string.VB.NET item Its usage
ProcessStartInfo This class stores information about the process you want to run.
FileName This is the program or filename you want to run.
You can set it to a file such as "example.txt" or an executable name.
Arguments Stores the arguments, including any -flags or filenames.
CreateNoWindow Specifies that you want to run a command line program silently without flashing a console window. Very useful for executing utility EXEs in the background.
WindowStyle Use this to set windows as hidden, with the ProcessWindowStyle.Hidden enumeration value.
UserName
WorkingDirectory
Domain These properties control OS-specific parameters, and are used in more complex situations where OS features are used extensively.

6. 总结

Here we saw several examples and practical uses of Process.Start in VB.NET. The author has used Process.Start in most programs he has written. It helps you offload difficult processing to other applications, and is a critical part of a well-design application.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值