Check with VB.Net Code whether a program is open or not

nomar2
nomar2 used Ask the Experts™
on
I am using VB.Net code

I have a piece of code built that needs another program open before it can run properly.

I know when the program is open it appears as SimplyAccounting.exe in Task Manager..is there a way I can test this...
if open continue...
if not open MsgBox "Program must be open"
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi, please see below for the code...

You can use it like this:

        If (IsProcessRunning("SimplyAccounting.exe")) Then
            MessageBox.Show("It is running")
        Else
            MessageBox.Show("Process not found...")
        End If
    Public Function IsProcessRunning(ByVal strProcess As String) As Boolean
        Dim pProcList() As Process
 
        ' Did user pass-in a valid process name?
        If (String.IsNullOrEmpty(strProcess)) Then
            ' No - return "not found"
            IsProcessRunning = False
            Exit Function
        End If
 
        ' Retrieve process list
        pProcList = Process.GetProcesses
 
        ' Traverse process list
        For Each pProcess As Process In pProcList
            ' Compare names
            Try
                If (pProcess.MainModule.ModuleName = strProcess) Then
                    ' We found it!
                    IsProcessRunning = True
                    Exit Function
                End If
            Catch
            End Try
        Next
 
        ' Not found...
        IsProcessRunning = False
    End Function

Open in new window

High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
You can shorten that SIGNIFICANTLY by using GetProcessesByName() instead.

Note that you do NOT include the ".exe" part of the process name:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If IsProcessRunning("SimplyAccounting") Then
            MessageBox.Show("Running")
        Else
            MessageBox.Show("Not Running")
        End If
    End Sub
 
    Public Function IsProcessRunning(ByVal AppNameWithoutPathOrExtension As String)
        Return Process.GetProcessesByName(AppNameWithoutPathOrExtension).Length > 0
    End Function

Open in new window

Author

Commented:
Just getting back to office will check this now ...Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial