如何在visual basic中使用process.start(use process.start in visual basic)

Process对象的Start方法可能是程序员最不受重视的工具之一。作为一个.NET方法,Start有一系列重载,这些重载是不同的参数集,它们精确地确定了该方法的功能。重载允许您指定在另一个进程启动时可能要传递给另一个进程的任何参数集。...

Process对象的Start方法可能是程序员最不受重视的工具之一。作为一个.NET方法,Start有一系列重载,这些重载是不同的参数集,它们精确地确定了该方法的功能。重载允许您指定在另一个进程启动时可能要传递给另一个进程的任何参数集。

Man using laptop computer

您可以使用Process.Start做什么实际上只受您可以使用的流程的限制。如果您想在记事本中显示基于文本的自述文件,只需执行以下操作:

Process.Start("ReadMe.txt") Process.Start("notepad", "ReadMe.txt")

本例假设自述文件与程序位于同一文件夹中,记事本是.txt文件类型的默认应用程序,并且它位于系统环境路径中。

process.start类似于vb6中的shell命令

对于熟悉VisualBasic6的程序员,Process.Start有点像VB6shell命令。在VB 6中,您将使用以下内容:

lngPID = Shell("MyTextFile.txt", vbNormalFocus)

使用process.start

您可以使用此代码启动记事本最大化,并创建ProcessStartInfo对象,以便进行更精确的控制:

Dim ProcessProperties As New ProcessStartInfoProcessProperties.FileName = "notepad"ProcessProperties.Arguments = "myTextFile.txt"ProcessProperties.WindowStyle = ProcessWindowStyle.MaximizedDim myProcess As Process = Process.Start(ProcessProperties)

启动隐藏进程

你甚至可以开始一个隐藏的过程。

ProcessProperties.WindowStyle = ProcessWindowStyle.Hidden

正在检索进程的名称

将Process.Start作为.NET对象使用可以提供很多功能。例如,您可以检索已启动的进程的名称。此代码将在输出窗口中显示“记事本”:

Dim myProcess As Process = Process.Start("MyTextFile.txt") Console.WriteLine(myProcess.ProcessName)This was something you could not do with the VB6 Shell command because it launched the new application asynchronously. Using WaitForExit can cause the reverse problem in .NET because you have to launch a process in a new thread if you need it to execute asynchronously. For example, if you need the components to remain active in a form where a process was launched and WaitForExit

One way to force the process to halt is to use the Kill method.

myProcess.Kill()

This code waits for ten seconds and then ends the process.

However, a forced delay is sometimes necessary to allow the process to complete exiting to avoid an error.

myProcess.WaitForExit(10000)' if the process doesn't complete within' 10 seconds, kill itIf Not myProcess.HasExited ThenmyProcess.Kill()End IfThreading.Thread.Sleep(1)Console.WriteLine("Notepad ended: " _& myProcess.ExitTime & _Environment.NewLine & _"Exit Code: " & _myProcess.ExitCode)

In most cases, it's probably a good idea to put your processing in a Using block to ensure that the resources used by the process are released.

Using myProcess As Process = New Process' Your code goes hereEnd Using

To make all this even easier to work with, there is even a Process component that you can add to your project so you can do a lot of the things shown above at design time instead of run time.

One of the things that this makes a lot easier is coding events raised by the process, such as the event when the process has exited. You can also add a handler using code like this:

' allow the process to raise eventsmyProcess.EnableRaisingEvents = True' add an Exited event handlerAddHandler myProcess.Exited, _AddressOf Me.ProcessExitedPrivate Sub ProcessExited(ByVal sender As Object, _ByVal e As System.EventArgs)' Your code goes hereEnd Sub

But simply selecting the event for the component is a lot easier.

  • 发表于 2021-09-11 23:49
  • 阅读 ( 233 )
  • 分类:编程

你可能感兴趣的文章

如何在excel中隐藏和取消隐藏工作表

需要整理Excel文档吗?下面是如何隐藏一张或多张床单。 ...

  • 发布于 2021-03-12 02:50
  • 阅读 ( 204 )

如何恢复excelvba密码

被密码保护的VBA文档卡住了?下面是如何撬锁。 ...

  • 发布于 2021-03-14 06:34
  • 阅读 ( 208 )

微软红心程序员!ms提供的11个免费开发工具

...的行业,有许多不同的语言和软件程序。也许你还不知道如何编码,但你想学习。 ...

  • 发布于 2021-03-22 05:32
  • 阅读 ( 397 )

visual basic语言(visual basic)和Visual C++(visual c++)的区别

Visual Basic与Visual C++的主要区别在于Visual Basic是面向对象的编程语言,Visual C++是集成开发环境(IDE)。 visualbasic是微软开发的一种用户友好的编程语言。在visualbasic.NET发布之前,它的最终版本是visualbasic6.0。另一方面,Visual C++是...

  • 发布于 2021-07-01 09:31
  • 阅读 ( 378 )

功能(function)和vb程序(procedure in vb)的区别

...代码返回值的过程,而过程是程序中的可执行语句块。 visualbasic(VB.NET)是在微软开发的.NET框架上实现的一种编程语言。它是一种现代的通用编程语言。它有助于开发高效的程序,而且更容易学习语言。此外,VB还提供了各种功...

  • 发布于 2021-07-01 18:59
  • 阅读 ( 273 )

建造(build)和在visual studio中重建(rebuild in visual studio)的区别

visualstudio中build和rebuild的主要区别在于,build有助于完成更改的代码文件,而rebuild删除所有以前编译过的文件并从头开始编译解决方案,而忽略以前所做的任何操作。 visualstudio是由微软开发的集成开发环境(IDE)。它提供了开...

  • 发布于 2021-07-02 00:11
  • 阅读 ( 393 )

功能(function)和程序(procedure)的区别

...用机制。两者之间的区别取决于编程语言的上下文。 在Visual Basic中,过程声明为- [AccessSpecifier]子过程重命名([ParameterList]) [声明] 末端接头 在Visual Basic中,函数声明为- [AccessSpecifier]函数名([ParameterList])_ 作为数据类型 [声...

  • 发布于 2021-07-13 20:48
  • 阅读 ( 278 )

vb语言(vb)和运动模拟(vb.net)的区别

关键区别:VB代表visualbasic。它是微软的一种高级编程语言,用于快速开发基于Windows的程序。VB.NET代表支持Visual Basic网络的技术。它是Microsoft的.NET framework语言之一。这种语言是专门为VB开发人员创建的。它属于在.NET框架上实现...

  • 发布于 2021-07-13 21:15
  • 阅读 ( 249 )

当日下载:microsoft express开发者工具

...学生开发人员的轻量级、易于使用且易于学习的工具”,Visual Studio Express版本包括:VisualWebDeveloper2005Express版SQLServer2005Express版Visual Basic 2005快速版Visual C#2005快速版Visual C++ 2005快速版Visual J#2005速成版看起来微软打算招聘更多的程...

  • 发布于 2021-08-03 01:11
  • 阅读 ( 145 )

visual basic术语表

...。Unix和Linux管理员也大量使用Perl来自动化维护工作。 process是指当前正在计算机上执行或“运行”的程序。 多态性是在面向对象编程解释中看到的一个词。这就是拥有两个不同类型的不同对象的能力,这两个对象都实现了相...

  • 发布于 2021-09-10 22:25
  • 阅读 ( 235 )
慕姜c84j
慕姜c84j

0 篇文章

相关推荐