How to open a Website in the default Browser in C# and VB.NET

To open a Website in the default Browser in C# and VB.NET you can use the snippet below.

Sample C#

System.Diagnostics.Process.Start("http://codesnippets.fesslersoft.de/");

Sample VB

System.Diagnostics.Process.Start("http://codesnippets.fesslersoft.de/")

How to get the executable filename in C# and VB.NET

To get the executable filename in C# and VB.NET you can use one the following methods.

Sample C#

//prefered way to retrieve exe filename
Console.WriteLine(Path.GetFileName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName));
 

Console.WriteLine(AppDomain.CurrentDomain.FriendlyName); //can cause problems when using click once, also this property does not always returns the right result.

Console.WriteLine(Path.GetFileName(Assembly.GetExecutingAssembly().Location)); //can fail when used in wcf application.
Console.WriteLine(Path.GetFileName(Assembly.GetExecutingAssembly().CodeBase)); //better if shadow copy feature is active, but can fail when used in wcf application
Console.WriteLine(Path.GetFileName(Assembly.GetEntryAssembly().Location));

Sample VB.NET

'prefered way to retrieve exe filename
Console.WriteLine(Path.GetFileName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName))


Console.WriteLine(AppDomain.CurrentDomain.FriendlyName)
'can cause problems when using click once, also this property does not always returns the right result.
Console.WriteLine(Path.GetFileName(Assembly.GetExecutingAssembly().Location))
'can fail when used in wcf application.
Console.WriteLine(Path.GetFileName(Assembly.GetExecutingAssembly().CodeBase))
'better if shadow copy feature is active, but can fail when used in wcf application
Console.WriteLine(Path.GetFileName(Assembly.GetEntryAssembly().Location))

How to exit an application in C# and VB.NET

To exit an application in C# and VB.NET you can use the following snippet.

Sample C#

System.Windows.Forms.Application.Exit(0); // WINFORM
Environment.Exit(0); // CONSOLE APP
Application.Shutdown(); // WPF

Sample VB.NET

System.Windows.Forms.Application.[Exit](0) ' WINFORM
Environment.[Exit](0) ' CONSOLE APP
Application.Shutdown() ' WPF

How to start a process and wait for exit in Powershell

To start a application and wait for exit in Powershell you can use the following snippet.

Sample Powershell

start-process -filepath "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe" -Wait -NoNewWindow -argumentlist '-args'

for more informations take a look at the MSDN Start-Process

How to kill a process in Batch

To kill a process in Batch you can use the snippets below.
If you dont know how to retrieve the PID you can use our other snippet List all Processes in Batch.

Sample Batches

taskkill /PID 1292

if you want to force the taskkill you can use /F.

taskkill /F /PID 1292

if you want to taskkill the process by name you can use the follwoing snippet.

taskkill /im notepad++.exe

for more informations see the msdn TASKKILL

How to kill a running process in C# and VB.NET

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

Sample C#

public static void KillProcess(string name, bool waitForExit = true)
{
	foreach (var process in Process.GetProcessesByName(name))
	{
		try
		{
			process.Kill();
			if (waitForExit)
			{
				process.WaitForExit();
			}
		}
		catch (Exception ex)
		{
			//handle the exception your way
		}

	}
}

Sample VB.NET

Public Shared Sub KillProcess(name As String, Optional waitForExit As Boolean = True)
	For Each process__1 As var In Process.GetProcessesByName(name)
		Try
			process__1.Kill()
			If waitForExit Then
				process__1.WaitForExit()
			End If
				'handle the exception your way
		Catch ex As Exception

		End Try
	Next
End Sub

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