3

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
Eric De Vault
  • 53
  • 1
  • 8
  • 1
    [Get folder path from Explorer window](http://stackoverflow.com/q/20960316/2596334) – Scott Solmer Nov 18 '14 at 20:26
  • I want to open C:\epds\WIP or if it is open I want to show it as current window, understand? vb not c#, I know there are convertors but that's not what I'm looking for. – Eric De Vault Nov 18 '14 at 20:49
  • I get it. First you look to see if Explorer is already open. If it is, check to see what folder it's browsed to (the link I provided). If it's where you want it, just [activate](http://stackoverflow.com/q/2315561/2596334) it. If not, open a new one. – Scott Solmer Nov 18 '14 at 20:58
  • Ok, thanks. I am wondering though, won't there always be at least one instance of explorer.exe and not necessarily the one in question? just wondering if that will cause conflicts. I will see if I can get what you linked to work. Thank you again. – Eric De Vault Nov 18 '14 at 21:02
  • If Explorer isn't open, it won't have a window. My first guess would be to use [findwindow](http://www.pinvoke.net/default.aspx/user32.findwindow) or maybe you could [try this](http://stackoverflow.com/a/2125610/2596334). – Scott Solmer Nov 18 '14 at 21:09

3 Answers3

3

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.

Misa Lazovic
  • 2,805
  • 10
  • 32
  • 38
PKanold
  • 139
  • 7
1

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
Creator
  • 1,502
  • 4
  • 17
  • 30
  • Much cleaner, thank you! Also, would there be a way to also focus on a folder within C:\epds\WIP\? The only other problem I'm running into is that folder inside WIP\ is always changing its name and as far as I know you cannot use wildcards for folder paths. So say I'm in C:\epds\WIP\SO123456 and I need to activate that window, it will not see it because I have pointed the folder path to a static C:\epds\WIP I guess I have too many variables to achieve my goal, thanks for your help @Creator – Eric De Vault Nov 20 '14 at 14:29
  • Hmmm, funny thing: if the subfolder in C:\epds\WIP\ is selected(Highlighted) in the explorer and it is not the active window, button4_Click navigates to that subfolder. If there is not a subfolder in the WIP folder then it operates as intended, or if the subfolder is not highlighted it operates as intended. – Eric De Vault Nov 20 '14 at 14:51
  • @EricDeVault its because you send the key ~ "enter" and with enter you open the folder or file. maybe try to use key right "SendKeys.SendWait("{RIGHT}")" or something that fits your needs."then it dos not open the selected folder inside your WIP folder" – Creator Nov 21 '14 at 03:35
  • Ahh, I see now. thanks for your help @Creator. What about subfolders in WIP that I would be in? would something like `Public tmpDir As New IO.DirectoryInfo("C:\epds\WIP\")` work? It's kind of like an or situation though, either the user is in \WIP or a subdirectory of WIP that needs called to focus – Eric De Vault Nov 21 '14 at 13:52
0

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!

Raj Rao
  • 8,872
  • 12
  • 69
  • 83