4 Replies - 5258 Views - Last Post: 02 February 2012 - 08:16 AM Rate Topic: -----

#1 mouse0270  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-January 12

How to get active Windows Explorer Directory

Posted 30 January 2012 - 07:40 AM

Alright, so the title, might sound a little weird, but here is what I am doing.

I wrote a program that runs in the background and waits for me to hit a predefined key combination.

When I hit this key combination I want it to do the following

Check and verify that Windows Explorer is up
If IE open, I want it to get the current/active windows explorer screen directory.

So lets say I had windows explorer opened to "c:\program files(x86)\my Folder"

Then when I press my key combination "shift+t" my program will get the directory of my windows explorer.

So it should display a message saying something like "Active Directory: c:\program files(x86)\my Folder"


Is this possible?

Is This A Good Question/Topic? 0
  • +

Replies To: How to get active Windows Explorer Directory

#2 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: How to get active Windows Explorer Directory

Posted 30 January 2012 - 07:54 AM

It's doable, but I think you're going to have to hook the window and also use low level keyboard hooking, but that's just for the combination. How you'll get the current directory I'm unsure but theirs got to be a windows API for it. All and all you'll need to learn to use windows API and if you're a beginner I suggest not attempting so just yet until you have a solid foundation on most things. You could just create your own explorer haha, then grab the current directory without windows API.

Code guru offers a decent tutorial.

http://www.codeguru....ad.php?t=417770
Was This Post Helpful? 0
  • +
  • -

#3 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: How to get active Windows Explorer Directory

Posted 30 January 2012 - 08:02 AM

The problem you are going to have is when there are multiple explorer windows open.

It is possible, however unlikely, that the user has multiple instances of explorer.exe open. You will have to figure out which one to use before worrying about how to actually get the directory out. The same thing goes when having multiple explorer windows open. The user may have My Docs, My Music etc etc open at the same time. I dont know how easy it would be to figure out which one is currently active and hook into that specific window in the specific process.

Wouldnt it be simpler to just use an OpenFolderDialog or OpenFileDialog?
Was This Post Helpful? 0
  • +
  • -

#4 mouse0270  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-January 12

Re: How to get active Windows Explorer Directory

Posted 30 January 2012 - 08:34 AM

The keyboard hooking is already done, I got that done first, the application runs very nicely in the background.

Maybe I should better describe what the program is supposed to do.

Alright, so my buddy is always complaining about creating new files. He looked at me the other day and said, I really wish I could just hit "shift+t" and get a prompt that will let me enter the name and select a file type.

Then itll automatically create a new file for me.


This seemed like a fun challenge, and honestly, everything but this getting the current windows explorer directory has been a breeze!

Right now there is a OpenFolderDialog, but I want to move away from this and make it more of in automatic process. Ill probably leave it just in case when I do get this to work, if it grabs the wrong directory, they can always change it.
Was This Post Helpful? 0
  • +
  • -

#5 mouse0270  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-January 12

Re: How to get active Windows Explorer Directory

Posted 02 February 2012 - 08:16 AM

Figured it our, thought I would give the code in case someone else would like to use it or comes across this thread.

First we need to created a new Dictionary, this is to store all of the windows explorer handles.
Public Class frmTrackWindowsForms
    Dim activeExplorerWindows As New Dictionary(Of String, String)
End Class


Well use this to keep track of all active windows explorer windows.

next we need to set up a sub-procedure that will update the Dictionaryso we always have the latest windows explorer windows in the Dictionary.
Public Class frmTrackWindowsForms
    Dim activeExplorerWindows As New Dictionary(Of String, String)

    Public Sub getActiveWindows()

        activeExplorerWindows.Clear() ' Clear out all data

	' Get All Explorer Windows (This is both windows explorer windows and Internet Explorer Windows.)
        Dim windowsShell As New SHDocVw.ShellWindows
        Dim explorerWindows As SHDocVw.InternetExplorer

	' Loop though all windows.
        For Each explorerWindows In windowsShell
            Try
		' Verify that window is windows explorer and not internet explorer
                If explorerWindows.document.ToString = "System.__ComObject" Then
                    ' Add explorer handle and location url of window to Dictionary
                    activeExplorerWindows.Add(explorerWindows.HWND, explorerWindows.LocationURL)
                End If
            Catch ex As Exception
		' Sometimes it errors out, if the run really fast! This prevents it crashing.
            End Try
        Next

    End Sub

End Class


Alright so now we have a sub procedure that well update all of our current windows, but we need to add a way to update this so we keep getting out most current windows, to me the easiest way was a nice a simple timer! Preferably set this to every second or so, maybe even milliseconds, this way you users wont usually move to fast for you not to get the most updated windows.
Public Class frmTrackWindowsForms
    Dim activeExplorerWindows As New Dictionary(Of String, String)

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Call getActiveWindows()
    End Sub

    Public Sub getActiveWindows()

        activeExplorerWindows.Clear() ' Clear out all data

	' Get All Explorer Windows (This is both windows explorer windows and Internet Explorer Windows.)
        Dim windowsShell As New SHDocVw.ShellWindows
        Dim explorerWindows As SHDocVw.InternetExplorer

	' Loop though all windows.
        For Each explorerWindows In windowsShell
            Try
		' Verify that window is windows explorer and not internet explorer
                If explorerWindows.document.ToString = "System.__ComObject" Then
                    ' Add explorer handle and location url of window to Dictionary
                    activeExplorerWindows.Add(explorerWindows.HWND, explorerWindows.LocationURL)
                End If
            Catch ex As Exception
		' Sometimes it errors out, if the run really fast! This prevents it crashing.
            End Try
        Next

    End Sub

End Class


Now your program will have an updated list of all windows explorer windows along with there Location stored a nice little Dictionary. Its up to you on how you plan to access or use this code. I used hotkeys, so when the user hit the hotkey it would check if the current window handle was in the collection if it was it would set the location URL to a global variable and let me work with that.

I hope this helps someone!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1