' Visual Basic
Dim myProcesses() as Process
Dim myProcess as Process
' Returns array containing all instances of "Notepad".
myProcesses() = Process.GetProcessesByName("Notepad")
For Each myProcess in myProcesses
myProcess.CloseMainWindow()
Next
Public Function RunningInstance() As Process
Dim current As Process
Try
current = Process.GetCurrentProcess
Dim processes As Process() = Process.GetProcessesByName(current.ProcessName)
'查找相同名的进程
Dim tmpProcess As Process
For Each tmpProcess In processes
'忽略当前进程
If tmpProcess.Id <> current.Id Then
'确认相同名的进程运行位置是否相同
If Reflection.Assembly.GetExecutingAssembly().Location.Replace("/", "\") = current.MainModule.FileName Then
'返回其它的运行实例
Return tmpProcess
End If
End If
Next tmpProcess
'没有其它实例,返加空值
Return Nothing
Catch ex As Exception
MsgBox(ex.Message , MsgBoxStyle.Critical, "Error")
End
Finally