VB如何判断exe文件是否被打开?

VB如何判断exe文件是否被打开?,第1张

遍历进程,获取进程的路径,从路径判断是不是你要的,如果是,关闭他.

如:

下面为vb2005

的例子.

Dim

p

As

New

Process

For

Each

p

In

Process.GetProcessesByName("notepad")

MsgBox(p.MainModule.FileName)

If

p.MainModule.FileName.ToLower

=

"c:\windows\system32\notepad.exe"

Then

p.Kill()

End

If

Next

看是否运行:

Private

Declare

Function

FindWindow

Lib

"user32"

Alias

"FindWindowA"

(ByVal

lpClassName

As

String,

ByVal

lpWindowName

As

String)

As

Long

Private

Sub

Command5_Click()

Dim

lHwnd

As

Long

lHwnd

=

FindWindow(vbNullString,

"程序的Title或Caption")

If

lHwnd

<>

0

Then

MsgBox

"程序正在运行!"

End

If

End

Sub

向它发送指令:

AppActivate

"程序的Title或Caption"

SendKeys

"指令"

要启动程序:

Call

Shell("完整路径和程序名称.exe")

Imports System.Runtime.InteropServices

Module Module1

Sub Main()

Dim instance As Process = RunningInstance()

If instance IsNot Nothing Then

ShowWindowAsync(instance.MainWindowHandle, 3) '调用api函数,正常显示窗口

Return

End If

Dim F1 As New Form1

Application.Run(F1) '显示窗体

End Sub

<DllImport("User32.dll")>

Public Function ShowWindowAsync(ByVal hWnd As System.IntPtr, ByVal cmdShow As Integer) As Boolean

End Function

Private Function RunningInstance() As Process '返回进程中已经打开的程序

Dim current As Process = Process.GetCurrentProcess()

Dim processes As Process() = Process.GetProcessesByName(current.ProcessName)

For Each process As Process In processes

If process.Id <>current.Id Then

If process.MainModule.FileName = current.MainModule.FileName Then

Return process

End If

End If

Next

Return Nothing

End Function

End Module


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7878237.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-10
下一篇 2023-04-10

发表评论

登录后才能评论

评论列表(0条)

保存