How to check if a process is running in C# and VB.NET

To check if a process is running in C# and VB.NET you can use the following snippet.

Sample C#

public static bool IsRunning(string processName)
{
	try
	{
		var retVal = Process.GetProcesses().Any(p => p.ProcessName.Contains(processName));
		return retVal;
	}
	catch (Exception ex)
	{
		//handle the exception your way 
		return false;
	}
}

public static bool IsRunning(this Process process)
{
	try
	{
		var retVal = Process.GetProcesses().Any(p => p.ProcessName.Contains(process.ProcessName));
		return retVal;
	}
	catch (Exception ex)
	{
		//handle the exception your way 
		return false;
	}
}

Sample VB.NET

Public Overloads Shared Function IsRunning(ByVal processName As String) As Boolean
        Try 
            Dim retVal As var = Process.GetProcesses.Any(() => {  }, p.ProcessName.Contains(processName))
            Return retVal
        Catch ex As Exception
            'handle the exception your way 
            Return false
        End Try
    End Function
    
    Public Overloads Shared Function IsRunning(this ByVal process As Process) As Boolean
        Try 
            Dim retVal As var = Process.GetProcesses.Any(() => {  }, p.ProcessName.Contains(process.ProcessName))
            Return retVal
        Catch ex As Exception
            'handle the exception your way 
            Return false
        End Try
    End Function

One thought on “How to check if a process is running in C# and VB.NET

Leave a Reply