4

I'd like to know how to grab the Path of the current active window using C#.

i get handle of currnet active window

        const int nChars = 256;
        int handle = 0;
        StringBuilder Buff = new StringBuilder(nChars);

        handle = GetForegroundWindow(); 

now how do i get path of this window?

i.e: Path of "my document" window is

C:\Users\User\Documents

-=-=-==-=-=edit-=-=-=-=-=-
i want to wirte program to monitor "windows explorer" and see Where the user goes?
(i.e:user go to c:\ and then go to program files and then go to Internet Explorer and i want to get this path:C:\Program Files\Internet Explorer. enter image description here

AminM
  • 1,658
  • 4
  • 32
  • 48
  • 3
    Windows don't have paths. A process using that window has a current path. You need to get the process. – stark Jul 14 '12 at 14:29
  • may be duplicate of http://stackoverflow.com/questions/2265647/how-can-i-get-the-exe-path-of-the-foreground-window – Ria Jul 14 '12 at 14:36
  • @Ria:this is not duplicate i want find a path of current window of "windows explorer".do you understand me?? – AminM Jul 14 '12 at 17:14
  • 1
    possible duplicate of [How to get full path of a window in windows explorer](http://stackoverflow.com/questions/5521003/how-to-get-full-path-of-a-window-in-windows-explorer) – tenfour Jul 14 '12 at 17:48

2 Answers2

7

Add a reference (COM) to "Microsoft Internet Controls"

var explorer = new SHDocVw.ShellWindowsClass().Cast<SHDocVw.InternetExplorer>().Where(hwnd => hwnd.HWND == handle).FirstOrDefault();
if (explorer != null) {
    string path = new Uri(explorer.LocationURL).LocalPath;
    Console.WriteLine("name={0}, path={1}", explorer.LocationName, path);
}

Prints the title/path of the explorer.exe instance with the main window handle in handle.

Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • VS2010 give me 2 error:The type 'SHDocVw.ShellWindowsClass' has no constructors defined and Interop type 'SHDocVw.ShellWindowsClass' cannot be embedded. Use the applicable interface instead – AminM Jul 14 '12 at 18:24
  • 1
    Disable 'embed interop types' in the reference's (shdocvw) properties. (You will need to distribute the interop dll) – Alex K. Jul 14 '12 at 18:27
  • 1
    I just had to change to SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows(); (from ShellWindowsClass()) In otherwords, use the interface instead of the Class. – blak3r Jan 08 '13 at 05:50
-1

use a thread...

                Exception threadEccezione = null;

                System.Threading.Thread staThread = new System.Threading.Thread(
                        delegate()
                        {
                            try
                            {
                                //SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();
                                var explorer = new SHDocVw.ShellWindowsClass().Cast<SHDocVw.InternetExplorer>().Where(hwnd => hwnd.HWND == handle).FirstOrDefault();

                                if (explorer != null)
                                {
                                    string path = new Uri(explorer.LocationURL).LocalPath;
                                    MessageBox.Show(path);
                                }
                            }
                            catch (Exception ex)
                            {
                                threadEccezione = ex;
                            }
                        }
                    );
                ;
                staThread.Start();
                staThread.Join();