0

I need to get the path of an open folder, I have a program that detects a key press and gets the Handle of the focused window when the press happend, Now the program will work only if the open window is a floder, I need to get the path of the currently open folder. I managed to get the path of the process of the folder (explorer.exe) but it`s not what I need... (c#)

How do I get the path of the folder using Handle or something else ?

thnx !

sitoNz
  • 93
  • 2
  • 9
  • I don`t have an Idea... I succeeded getting the name of the window and try to get the path with it but not luck – sitoNz Dec 16 '15 at 08:00
  • [You can't](http://stackoverflow.com/questions/20960316/get-folder-path-from-explorer-window). This is an XY problem anyway. Explain **why** you need this. – CodeCaster Dec 16 '15 at 08:01
  • I want to make a shortcut, that when you press a button when a folder is in focus, I`ll edit the path of the folder and open a new one in another path... @CodeCaster – sitoNz Dec 16 '15 at 08:02
  • This is purely for explorer? What about other file managers? Did you explore the shell API or are intent on hacking at this? – David Heffernan Dec 16 '15 at 08:45
  • @DavidHeffernan It`s purely for folders of explorer nothing else – sitoNz Dec 16 '15 at 08:48
  • @DavidHeffernan I`m looking on some of the functions now, trying to look how it can help me edit : can`t find something helpful :( – sitoNz Dec 16 '15 at 08:53
  • [IPersistFolder2::GetCurFolder](https://msdn.microsoft.com/en-us/library/windows/desktop/bb775340.aspx) returns that information. [Querying information from an Explorer window](https://blogs.msdn.microsoft.com/oldnewthing/20040720-00/?p=38393/) shows, how to get to the interface. – IInspectable Dec 16 '15 at 09:25
  • @IInspectable Thank you for the help, I think I can find something to work out but the 'Querying information from an Explorer window' is in c++ and I`m programming in c# – sitoNz Dec 16 '15 at 10:38
  • The code uses COM. COM is language-agnostic. Just use it from C# then. The code will look a lot easier, although getting the IDispatch interface is [a bit more involved](http://stackoverflow.com/a/8068797/1889329). – IInspectable Dec 16 '15 at 10:48

1 Answers1

2

Add a COM reference to shell32.dll & shdocvw.dll

string path = null;
foreach (SHDocVw.InternetExplorer window in new SHDocVw.ShellWindows()) {
    if (your_known_explorer_HWND == window.HWND) {
        path = new Uri(window.LocationURL).LocalPath);
        break;
    }
}
Alex K.
  • 171,639
  • 30
  • 264
  • 288