Marius B Marius B - 1 month ago 11
C# Question

How to get full paths of all the folders that are currently open in Windows Explorer with C#?

I want to get full paths of all the folders that are currently open in Windows Explorer. I already managed to get all the handles of those folders in the following format:

IntPtr WindowInformation.Handle


Is it possible to get similar list as this one from these handles or do I need to use another approach?


C:\test\folder01\

C:\test\folder02\

D:\Games\


I found several answers to similar questions, but some of them returned only the name of the folder and the other one only works if some files are selected in the folder. I need a list of simply ALL currently open folders. I hope it's possible, I never used WinAPI before.

P.S.: Windows Vista/7/8/10 is ok, I don't need it to work on the older ones.

Answer

They are available via the Shell APIs window enumeration (along with Internet Explorer windows) so try this.

Add a COM reference to shell32/shdocvw.dll and:

foreach (SHDocVw.InternetExplorer window in new SHDocVw.ShellWindows()) {
    if (Path.GetFileNameWithoutExtension(window.FullName).ToLowerInvariant() == "explorer") {
        if (Uri.IsWellFormedUriString(window.LocationURL, UriKind.Absolute))
            Console.WriteLine(new Uri(window.LocationURL).LocalPath);
    }
}
Comments