Solved

VB check if another program is running

Posted on 2013-02-05
12
352 Views
Last Modified: 2016-02-28
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
Comment
Question by:deleyd
  • 3
  • 2
  • 2
  • +3
12 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 38855780
Process.GetProcessesByName("MyProgram")

Open in new window

0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38855781
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
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 38855789
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38855792
You would need that line of code into a condition

If Process.GetProcessesByName("MyProgram").Count = 0 Then
    'Launch it
End If
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 38855795
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
 
LVL 42

Expert Comment

by:sedgwick
ID: 38855822
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
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 38855826
Too much traffic !! :)
0
 

Expert Comment

by:user1010
ID: 41479546
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
 

Author Comment

by:deleyd
ID: 41482061
Was the error, "The system cannot find the file specified"?

Need to first check that file firefox.exe actually exists.
0
 

Expert Comment

by:user1010
ID: 41483382
checkFFox.vbs(1, 14) Microsoft VBScript compilation error: Expected end of statement
0
 

Author Comment

by:deleyd
ID: 41484110
https://msdn.microsoft.com/en-us/library/8dy0ah20.aspx
Can you post lines 13 & 14 of the code?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 41484668
@user1010, the posted solutions are in VB.Net, which doesn't work in VBScript...
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question