• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 362
  • Last Modified:

VB check if another program is running

In Visual Basic I want to check if a certain program is running, e.g. C:\MyProgram.exe

(If not then I'll go ahead and launch it, which I already have the code for, but I don't want to launch it if it's already running. I don't want to launch a 2nd copy.)

Code?
0
deleyd
Asked:
deleyd
  • 3
  • 2
  • 2
  • +3
1 Solution
 
Meir RivkinFull stack Software EngineerCommented:
Process.GetProcessesByName("MyProgram")

Open in new window

0
 
Meir RivkinFull stack Software EngineerCommented:
use the application name without the extension (exe).
btw, u need the following namespace to use Process:
using System.Diagnostics;

Open in new window

0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
therei a function for that
Public Function IsProcessRunning(name As String) As Boolean
	
	For Each clsProcess As Process In Process.GetProcesses()
		If clsProcess.ProcessName.StartsWith(name) Then
			'process found so it's running so return true
			Return True
		End If
	Next
		Return False
End Function

Open in new window

Raise a messagebox end exit the sub if it running...
Hope it helps
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
CodeCruiserCommented:
You would need that line of code into a condition

If Process.GetProcessesByName("MyProgram").Count = 0 Then
    'Launch it
End If
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
As sedgwick demonstrated, the GetProcessesByName() method expects the filename without a path and without any extension.  Here's  another example:
        Dim FullPath As String = "C:\MyProgram.exe"
        Dim FileName As String = System.IO.Path.GetFileNameWithoutExtension(FullPath)
        If Process.GetProcessesByName(FileName).Length = 0 Then
            Process.Start(FullPath)
        End If

Open in new window

0
 
Meir RivkinFull stack Software EngineerCommented:
here's the complete code which bring to front the application if already is running:

Private Declare Function ShowWindow Lib "user32" _
(ByVal hWnd As System.IntPtr, ByVal nCmdShow As Long) As Long

Private Const SW_RESTORE = 9


if(Process.GetProcessesByName("MyProgram").Count() == 0){
//launch the program
}else{
ShowWindow(proc.MainWindowHandle, SW_RESTORE)
}

Open in new window

0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
Too much traffic !! :)
0
 
user1010Commented:
I tried this code to check if a web browser is running but I got error.

Dim FullPath As String = "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
        Dim FileName As String = System.IO.Path.GetFileNameWithoutExtension(FullPath)
        If Process.GetProcessesByName(FileName).Length = 0 Then
            Process.Start(FullPath)
        End If

Open in new window


Am I missing some code?
0
 
deleydAuthor Commented:
Was the error, "The system cannot find the file specified"?

Need to first check that file firefox.exe actually exists.
0
 
user1010Commented:
checkFFox.vbs(1, 14) Microsoft VBScript compilation error: Expected end of statement
0
 
deleydAuthor Commented:
https://msdn.microsoft.com/en-us/library/8dy0ah20.aspx
Can you post lines 13 & 14 of the code?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
@user1010, the posted solutions are in VB.Net, which doesn't work in VBScript...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 3
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now