Code:
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
Public Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) As Boolean
End Function
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim hWnd As IntPtr = FindWindowByExe("notepad.exe")
If Not hWnd = IntPtr.Zero Then ' found
SetForegroundWindow(hWnd) ' bring to foreground
Else
Process.Start("notepad.exe")
End If
End Sub
Private Function FindWindowByExe(ByVal Filename As String) As IntPtr
Filename = System.IO.Path.GetFileNameWithoutExtension(Filename)
Dim procList() As Process = Process.GetProcesses
For Each p As Process In procList
If p.ProcessName.ToLower = Filename.ToLower Then
Return p.MainWindowHandle
End If
Next
Return IntPtr.Zero
End Function
End Class