locked
Get File Explorer All Opened Folders Path in VB.Net RRS feed

  • Question

  • How to get Windows File Explorer all opened Windows Path -

    like C:\, E:\AndrewBlues, E:\AndrewJazz, C:\Users, Computer, Libraries\Documents,

    C:\Users\Andrew\Documents\Visual Studio 2008\Projects\WindowsApplication1, etc.

    please help, thanks in advance.

    Thursday, January 16, 2014 7:02 PM

Answers

  • How to get Windows File Explorer all opened Windows Path -

    like C:\, E:\AndrewBlues, E:\AndrewJazz, C:\Users, Computer, Libraries\Documents,

    C:\Users\Andrew\Documents\Visual Studio 2008\Projects\WindowsApplication1, etc.

    please help, thanks in advance.

    I guess this is what you mean. If you try to use Option Strict On with it there is an error that Option Strict On does not like even though the code compiles without Option Strict On. I never looked into correcting that and I got the code from that blogspot.

    Also, if I remember correctly, if a folder is opened but contains nothing in it then it will not show up in the list for some reason.

    Imports Shell32 ' Add reference browse C:\Windows\System32\Shell32.dll or com Microsoft Shell Controls and Automation
    
    'http://xkom.blogspot.com/2011/06/get-opened-folder-location-in-explorer.html
    
    Public Class Form1
    
        Dim Lb As New ListBox
    
        Dim WithEvents RoutineTimer As New Timer
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Lb.Parent = Me
            Lb.Dock = DockStyle.Fill
            Me.Text = "Opened Folder by user (explorer)"
            RoutineTimer.Interval = 1000
            RoutineTimer.Start()
    
        End Sub
    
        Sub GetOpenedFolder()
    
            Dim MShell As New Shell
            Dim SFV As ShellFolderView
    
            Lb.Items.Clear()
            On Error Resume Next
            For Each o In MShell.Windows
                If TypeName(o.document) <> "HTMLDocument" Then
                    SFV = o.document
                    If SFV.Folder.Items.Count > 0 Then
                        Lb.Items.Add(TrimPath(CType(SFV.Folder.Items(0), ShellFolderItem).Path))
                    End If
                End If
            Next
    
        End Sub
    
        Sub Timer_Job() Handles RoutineTimer.Tick
            GetOpenedFolder()
        End Sub
    
        Function TrimPath(ByRef s As String) As String
            Return s.Remove(InStrRev(s, "\"))
        End Function
    
    End Class


    Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.


    • Edited by Mr. Monkeyboy Thursday, January 16, 2014 8:11 PM 5555
    • Proposed as answer by ArifMustafa Friday, January 17, 2014 9:12 AM
    • Marked as answer by Leo (Apple) Yang Thursday, January 23, 2014 5:21 AM
    Thursday, January 16, 2014 8:10 PM
  • Still there are many folders Opened by Explorer, not showing in the list

    i.e. My Computer, ReCycle Bin, Control Panel etc,

    pls. help sir....

    Hi,

     Here is a way to get the text from the explorer windows Address combobox in WinXP. This may not work on newer OS versions, i don`t know because i only have XP. So you may need to change the search pattern of the class names and/or the tree of children to find the combobox that has the text in it. You can use Spy++ to find the class names an the tree order of the windows to the combobox if needed. The text returned is also going to depend on the Folder Option Settings that are set in the explorer window also. It may not be set to show the full path in the address bar. You can try it if you want.  :)

    Imports System.Runtime.InteropServices
    Imports System.Text
    
    Public Class Form1
        Private Const WM_GETTEXT As Integer = &HD
        Private Const WM_GETTEXTLENGTH As Integer = &HE
    
        <DllImport("user32.dll", EntryPoint:="FindWindowExW")> _
        Private Shared Function FindWindowExW(ByVal hwndParent As System.IntPtr, ByVal hwndChildAfter As System.IntPtr, <InAttribute(), MarshalAs(UnmanagedType.LPTStr)> ByVal lpszClass As String, <InAttribute(), MarshalAs(UnmanagedType.LPTStr)> ByVal lpszWindow As String) As System.IntPtr
        End Function
    
        <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
        Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As Integer, ByVal lParam As StringBuilder) As Integer
        End Function
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ListBox1.Items.Clear()
            Dim hWinList As New List(Of IntPtr)
    
            'Get Each Explorer Windows Handle
            Dim hWnd As IntPtr = FindWindowExW(IntPtr.Zero, IntPtr.Zero, "CabinetWClass", Nothing)
            While Not hWnd.Equals(IntPtr.Zero)
                hWinList.Add(hWnd)
                hWnd = FindWindowExW(IntPtr.Zero, hWnd, "CabinetWClass", Nothing)
            End While
    
            'Loop threw each explorer window in the list and get the text from the Address combobox
            If hWinList.Count > 0 Then
                For Each hChld As IntPtr In hWinList
                    Dim hChild1 As IntPtr = FindWindowExW(hChld, IntPtr.Zero, "WorkerW", Nothing)
                    Dim hChild2 As IntPtr = FindWindowExW(hChild1, IntPtr.Zero, "ReBarWindow32", Nothing)
                    Dim hChild3 As IntPtr = FindWindowExW(hChild2, IntPtr.Zero, "ComboBoxEx32", Nothing)
                    Dim len As Integer = SendMessage(hChild3, WM_GETTEXTLENGTH, 0, Nothing)
                    Dim sb As New StringBuilder(len + 1)
                    SendMessage(hChild3, WM_GETTEXT, len + 1, sb)
                    ListBox1.Items.Add(sb.ToString)
                Next
            End If
    
        End Sub
    End Class

    Saturday, January 18, 2014 3:44 PM

All replies

  • How to get Windows File Explorer all opened Windows Path -

    like C:\, E:\AndrewBlues, E:\AndrewJazz, C:\Users, Computer, Libraries\Documents,

    C:\Users\Andrew\Documents\Visual Studio 2008\Projects\WindowsApplication1, etc.

    please help, thanks in advance.

    I guess this is what you mean. If you try to use Option Strict On with it there is an error that Option Strict On does not like even though the code compiles without Option Strict On. I never looked into correcting that and I got the code from that blogspot.

    Also, if I remember correctly, if a folder is opened but contains nothing in it then it will not show up in the list for some reason.

    Imports Shell32 ' Add reference browse C:\Windows\System32\Shell32.dll or com Microsoft Shell Controls and Automation
    
    'http://xkom.blogspot.com/2011/06/get-opened-folder-location-in-explorer.html
    
    Public Class Form1
    
        Dim Lb As New ListBox
    
        Dim WithEvents RoutineTimer As New Timer
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Lb.Parent = Me
            Lb.Dock = DockStyle.Fill
            Me.Text = "Opened Folder by user (explorer)"
            RoutineTimer.Interval = 1000
            RoutineTimer.Start()
    
        End Sub
    
        Sub GetOpenedFolder()
    
            Dim MShell As New Shell
            Dim SFV As ShellFolderView
    
            Lb.Items.Clear()
            On Error Resume Next
            For Each o In MShell.Windows
                If TypeName(o.document) <> "HTMLDocument" Then
                    SFV = o.document
                    If SFV.Folder.Items.Count > 0 Then
                        Lb.Items.Add(TrimPath(CType(SFV.Folder.Items(0), ShellFolderItem).Path))
                    End If
                End If
            Next
    
        End Sub
    
        Sub Timer_Job() Handles RoutineTimer.Tick
            GetOpenedFolder()
        End Sub
    
        Function TrimPath(ByRef s As String) As String
            Return s.Remove(InStrRev(s, "\"))
        End Function
    
    End Class


    Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.


    • Edited by Mr. Monkeyboy Thursday, January 16, 2014 8:11 PM 5555
    • Proposed as answer by ArifMustafa Friday, January 17, 2014 9:12 AM
    • Marked as answer by Leo (Apple) Yang Thursday, January 23, 2014 5:21 AM
    Thursday, January 16, 2014 8:10 PM
  • Dear,

    you can also use Lib "kernel32" - 

    GetFullPathName function

    & do Declaration like this, well but it's good that you first read carefully above function from

    msdn page, 

    enjoy,...


    Pl. Mark/Proposed as Answer if found your solution Vote as Helpful if related to your topic. Always Motivate others by performing this Action.

    Hi,

     This is not what the OP is looking for. The OP wants to get the path of where any opened explorer windows are opened to. And just to let you know the Declaration in that link is for outdated VB6 and will not work in VB.NET. PInvoke.net is a good place to find VB.NET declarations for API functions and structures.    :)

    Friday, January 17, 2014 3:59 AM
  • Hi,

     This is not what the OP is looking for. The OP wants to get the path of where any opened explorer windows are opened to. And just to let you know the Declaration in that link is for outdated VB6 and will not work in VB.NET. PInvoke.net is a good place to find VB.NET declarations for API functions and structures.    :)

    Yeah, you r right, still i have to study VB6 for my further university activities, well all is well due to you,

    nd thanks for opening morning sleepy eyes, thanks again. :D


    Pl. Mark/Proposed as Answer if found your solution Vote as Helpful if related to your topic. Always Motivate others by performing this Action.

    Friday, January 17, 2014 9:09 AM
  • Still there are many folders Opened by Explorer, not showing in the list

    i.e. My Computer, ReCycle Bin, Control Panel etc,

    pls. help sir....

    Saturday, January 18, 2014 2:41 PM
  • Still there are many folders Opened by Explorer, not showing in the list

    i.e. My Computer, ReCycle Bin, Control Panel etc,

    pls. help sir....

    Hi,

     Here is a way to get the text from the explorer windows Address combobox in WinXP. This may not work on newer OS versions, i don`t know because i only have XP. So you may need to change the search pattern of the class names and/or the tree of children to find the combobox that has the text in it. You can use Spy++ to find the class names an the tree order of the windows to the combobox if needed. The text returned is also going to depend on the Folder Option Settings that are set in the explorer window also. It may not be set to show the full path in the address bar. You can try it if you want.  :)

    Imports System.Runtime.InteropServices
    Imports System.Text
    
    Public Class Form1
        Private Const WM_GETTEXT As Integer = &HD
        Private Const WM_GETTEXTLENGTH As Integer = &HE
    
        <DllImport("user32.dll", EntryPoint:="FindWindowExW")> _
        Private Shared Function FindWindowExW(ByVal hwndParent As System.IntPtr, ByVal hwndChildAfter As System.IntPtr, <InAttribute(), MarshalAs(UnmanagedType.LPTStr)> ByVal lpszClass As String, <InAttribute(), MarshalAs(UnmanagedType.LPTStr)> ByVal lpszWindow As String) As System.IntPtr
        End Function
    
        <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
        Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As Integer, ByVal lParam As StringBuilder) As Integer
        End Function
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ListBox1.Items.Clear()
            Dim hWinList As New List(Of IntPtr)
    
            'Get Each Explorer Windows Handle
            Dim hWnd As IntPtr = FindWindowExW(IntPtr.Zero, IntPtr.Zero, "CabinetWClass", Nothing)
            While Not hWnd.Equals(IntPtr.Zero)
                hWinList.Add(hWnd)
                hWnd = FindWindowExW(IntPtr.Zero, hWnd, "CabinetWClass", Nothing)
            End While
    
            'Loop threw each explorer window in the list and get the text from the Address combobox
            If hWinList.Count > 0 Then
                For Each hChld As IntPtr In hWinList
                    Dim hChild1 As IntPtr = FindWindowExW(hChld, IntPtr.Zero, "WorkerW", Nothing)
                    Dim hChild2 As IntPtr = FindWindowExW(hChild1, IntPtr.Zero, "ReBarWindow32", Nothing)
                    Dim hChild3 As IntPtr = FindWindowExW(hChild2, IntPtr.Zero, "ComboBoxEx32", Nothing)
                    Dim len As Integer = SendMessage(hChild3, WM_GETTEXTLENGTH, 0, Nothing)
                    Dim sb As New StringBuilder(len + 1)
                    SendMessage(hChild3, WM_GETTEXT, len + 1, sb)
                    ListBox1.Items.Add(sb.ToString)
                Next
            End If
    
        End Sub
    End Class

    Saturday, January 18, 2014 3:44 PM
  • Still there are many folders Opened by Explorer, not showing in the list

    i.e. My Computer, ReCycle Bin, Control Panel etc,

    pls. help sir....

    I didn't realize that "My Computer" or "Control Panel" were "Folders".


    Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.

    Saturday, January 18, 2014 4:02 PM