Reputation: 53
This seems like a simple task but I cannot seem to produce the results looking for.
currently I have this code
Dim folderpath As String
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
folderpath = "C:\epds\WIP"
Process.Start("explorer.exe", folderpath)
End Sub
That's fine and it opens my folder path as indicated, however, if the instance of that folder is already open in the explorer how do I just make that window current instead of opening a new window explorer?
EDIT: This seemed to do the trick, thanks for pointing me in the right direction @Okuma.Scott
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindow( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As IntPtr
End Function
<DllImport("user32.dll", EntryPoint:="FindWindow", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindowByClass( _
ByVal lpClassName As String, _
ByVal zero As IntPtr) As IntPtr
End Function
<DllImport("user32.dll", EntryPoint:="FindWindow", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindowByCaption( _
ByVal zero As IntPtr, _
ByVal lpWindowName As String) As IntPtr
End Function
Dim folderpath As String
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
folderpath = "C:\epds\WIP"
'Process.Start("explorer.exe", folderpath)
Dim nWnd As IntPtr
Dim ceroIntPtr As New IntPtr(0)
Dim Wnd_name As String
Wnd_name = "WIP"
nWnd = FindWindow(Nothing, Wnd_name)
'show the info
If nWnd.Equals(ceroIntPtr) Then
Process.Start("explorer.exe", folderpath)
Else
AppActivate(Wnd_name)
SendKeys.SendWait("~")
End If
End Sub
Upvotes: 3
Views: 5980
Reputation: 9138
This works and will not open multiple windows:
Process.Start(new ProcessStartInfo() { FileName = "C:\\", UseShellExecute = true });
The only downside is that it does not bring the opened folder to the foreground (which depending on your use case may or may not be a bad thing!
Upvotes: 0
Reputation: 148
I was trying to solve this same issue and discovered that it seems to work by just calling Process.Start with the desired path:
Process.Start("C:\Temp")
If the folder is already open in an Explorer window it opens the existing window otherwise it opens a new window.
Upvotes: 3
Reputation: 1512
you need to import Imports System.Runtime.InteropServices
then you can use the function Findwindow
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
End Function
Then make 2 dims 1. folderpath and 2 is foldername
Then in your click event use "System.IO.Path.GetFileName(folderpath)" to get the name of the window you are looking for." for you WIP"
Then check with a if statement if FindWindow(vbNullString, foldername) = 0 "not open"
The vbNullString Represents a zero-length string for print and display functions, and for calling external procedures."msdn"
so if findwindow is 0 open the folder and else focus the folder
Dim folderpath As String
Dim foldername As String
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
folderpath = "C:\epds\WIP"
foldername = System.IO.Path.GetFileName(folderpath)
If FindWindow(vbNullString, foldername) = 0 Then
Process.Start("explorer.exe", folderpath)
Else
AppActivate(foldername)
SendKeys.SendWait("~")
End If
End Sub
Upvotes: 1