We help IT Professionals succeed at work.

Finding out if an exe is running

Friends,

I am trying to determine if an exe is already running on a machine.

Right now, i do the following:

If ProcessesRunning("PredictiveLapAnalysis.exe") > 1 Then
                                ProcessKill("PredictiveLapAnalysis.exe")
                                Process.Start("PredictiveLapAnalysis.exe")
                            Else
                                Process.Start("PredictiveLapAnalysis.exe")
                            End If


So as you can see, I call the ProcessesRunning sub to determine if a particular exe is running.

    Public Function ProcessesRunning(ByVal ProcessName As String) As Integer
        Try
            Return Process.GetProcessesByName(ProcessName).GetUpperBound(0) + 1
        Catch
            Return 0
        End Try
    End Function

However unfortunately, it always returns 0

What am I doing wrong?  Is there a better way to do this within VB.net (VS2008) and XP?  Yes I am an administrator on the target machine.

Looking for VB.net sytax help please!

Thanks in advance!

Best Regards,
Eric
Comment
Watch Question

High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
Do NOT include the ".exe" part and it should work...  =)

Author

Commented:
I'll Try it!  Thanks!
Shahan AyyubSenior Software Engineer

Commented:
try this one also:

       If ProcessesRunning("PredictiveLapAnalysis.exe") < 1 Then
            Process.Kill("PredictiveLapAnalysis.exe")
            Process.Start("PredictiveLapAnalysis.exe")
        Else
            Process.Start("PredictiveLapAnalysis.exe")
        End If

    Public Function ProcessesRunning(ByVal ProcessName As String) As Integer
        Try
            Return Process.GetProcessesByName(ProcessName).GetUpperBound(0) + 1
        Catch
            Return 0
        End Try
    End Function

Author

Commented:
Idle_Mind,

Yep, that worked!  Thank you!

Author

Commented:
Good Answer!