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.