0

For example on the task bar i make right mouse click on the File Explorer > File Explorer > Open and now for example i'm in C:\

Now i want somehow using the csharp code to get this File Explorer opened window directory in this case C:\ and if i will open a new window of File Explorer and will go to c:\temp and i will run the program again now i will have array or list of two strings of the two paths: Window 1: C:\ Window 2: C:\Temp

What i tried until now:

At top of form1:

[DllImport("user32.dll", SetLastError = true)]
 static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

Then in constructor calling GetDirs:

public Form1()
        {
            InitializeComponent();

            GetDirs();
        }

Then the method GetDirs:

private void GetDirs()
        {
            IntPtr MyHwnd = FindWindow(null, "Directory");
            var t = Type.GetTypeFromProgID("Shell.Application");
            dynamic o = Activator.CreateInstance(t);
            try
            {
                var ws = o.Windows();
                for (int i = 0; i < ws.Count; i++)
                {
                    var ie = ws.Item(i);
                    if (ie == null || ie.hwnd != (long)MyHwnd) continue;
                    var path = System.IO.Path.GetFileName((string)ie.FullName);
                    if (path.ToLower() == "explorer.exe")
                    {
                        var explorepath = ie.document.focuseditem.path;
                    }
                }
            }
            finally
            {
                Marshal.FinalReleaseComObject(o);
            } 
        }

But it's never get to the line:

var explorepath = ie.document.focuseditem.path;

And i'm not sure what type of var is explorepath.

TheLost Lostit
  • 505
  • 6
  • 28
  • Is ever `path` the executable name with extension? Maybe its only `explorer` or `C:\Windows\explorer.exe`. To see what var is explorerpath just put you mouse on `ie.document.focuseditem.path;` – Pau C Sep 04 '16 at 09:33

2 Answers2

3

To get the focused item's directorynames in all your open explorer windows:

        var t = Type.GetTypeFromProgID("Shell.Application");
        dynamic o = Activator.CreateInstance(t);
        try
        {
            var ws = o.Windows();
            for (int i = 0; i < ws.Count; i++)
            {
                var ie = ws.Item(i);
                if (ie == null) continue;
                var path = System.IO.Path.GetFileName((string)ie.FullName);
                if (path.ToLower() == "explorer.exe")
                {
                    var explorepath = System.IO.Path.GetDirectoryName(ie.document.focuseditem.path);
                }
            }
        }
        finally
        {
            Marshal.FinalReleaseComObject(o);
        }

I'm not sure what you are trying to achieve with FindWindow, which is basically what I removed.

rlee
  • 293
  • 1
  • 8
  • I'm getting in explorepath the first sub directory. How can i get only the main directory ? For example if the File Explorer window is opened in c:\ what i get in explorepath is C:\\$Recycle.Bin or if it's opened in C:\Temp i'm getting explorepath C:\\Temp\\base but what i want to get is only C:\\Temp or only C:\\ and not the first sub directory. – TheLost Lostit Sep 04 '16 at 10:34
  • ok, updated my answer to get the directory name of the focused item, is that what you want? – rlee Sep 04 '16 at 12:34
  • Yes this is what i needed. Thank you. – TheLost Lostit Sep 04 '16 at 13:48
1

Did you try with Microsoft Internet Controls SHDocVw? For example, this code listing can show the path of currently opened directories.

SHDocVw.ShellWindows shellWindows = null;
try
{
    shellWindows = new SHDocVw.ShellWindows();
    foreach (Set_folder_view_2.WinAPI._IServiceProvider serviceProvider in shellWindows)
    {
        SHDocVw.InternetExplorer ie = (SHDocVw.InternetExplorer)serviceProvider;

        if (Path.GetFileNameWithoutExtension(ie.FullName).Equals("explorer", StringComparison.OrdinalIgnoreCase))
        {
            Console.WriteLine(ie.LocationURL);
        }
    }
}
catch (Exception ex)
{
    Console.WriteLine(ex);
}
finally
{
    if (shellWindows != null)
    {
        Marshal.ReleaseComObject(shellWindows);
    }
}
T N
  • 396
  • 5
  • 13