In VB 6, we had a very friendly App object which would give us lot of information regarding the current application. If one wanted to check if there is already an instance of this application running it was just two lines of code. In VB.NET 2002/2003 we do not have any such object so to check whether we already have an instance of the application running we need to do some extra work.

Here is a function which checks if the instance of the application is already running. If returns a Process object which contains information about the currently running instance or a null value(if the application is not running).
VB Code:
  1. 'Function : checkInstance
  2.     'Input Parameter : None
  3.     'Return Value : Returns the Process if the exe
  4.     'is already running or else returns nothing
  5.     Public Function checkInstance() As Process
  6.         Dim cProcess As Process = process.GetCurrentProcess()
  7.         Dim aProcesses() As Process = process.GetProcessesByName(cProcess.ProcessName)
  8.         'loop through all the processes that are currently running on the
  9.         'system that have the same name
  10.         For Each process As Process In aProcesses
  11.             'Ignore the currently running process
  12.             If process.Id <> cProcess.Id Then
  13.                 'Check if the process is running using the same EXE as this one
  14.                 If [Assembly].GetExecutingAssembly().Location = cProcess.MainModule.FileName Then
  15.                     'if so return to the calling function with the instance of the process
  16.                     Return process
  17.                 End If
  18.             End If
  19.         Next
  20.         'if nothing was found then this is the only instance, so return null
  21.         Return Nothing
  22.     End Function
We just have to call this function at the entry point of our application. If the return value is Null then this is the first instance and if it returns a process then we already have an instance of the application running on the system. Ideally a project will be started from Sub Main, so I would put the code in the Sub Main procedure.
VB Code:
  1. Public Sub main()
  2.         Dim tempProcess As Process
  3.         tempProcess = checkInstance()
  4.         If tempProcess Is Nothing Then
  5.             Application.Run(New Form1)
  6.         Else
  7.             MessageBox.Show("Application is already running")
  8.         End If
  9.     End Sub
In case you want to set focus to the currently running instance then you can get the MainWindowHandle of the already running instance like this
VB Code:
  1. tempProcess.MainWindowHandle
And then use SetForeGroundWindow Api to activate the already running instance.

Don't forget to Import System.Diagnostics namespace.