Processing Ajax...

Title

Message

Confirm

Confirm

Confirm

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure?

User Image
Christian Treffler
53 discussion posts
Hello,

I want to write a scripted Function which does some stuff whenever I open a link to a folder. I set a trigger to run that function whenever a windows is created with Process Filename 'C:\Windows\explorer.exe' and Window Class "CabinetWClass". So far, so good.

But now I want to know the path to the folder which is shown in that window. One solution would be:

Code

public static class DisplayFusionFunction
{
    public static void Run(IntPtr windowHandle)
    {
        if (BFS.Input.IsKeyDown("16")) // Run only, if the Shift key is pressed during start of this function
        {
                BFS.Dialog.ShowMessageInfo(BFS.Window.GetText(windowHandle));
        }
    }
}


But that only works as intended, if I activate the option 'Show complete path in the title bar' in Windows.
If that option is not activated, I only get the name of the folder, not the complete path.
Is there another way to do this?

Thanks,
Christian
Aug 5, 2021  • #1
User Image
Christian Treffler
53 discussion posts
I found way to do it. If anybody is interested:

Code

using System;
using System.Threading.Tasks;
using System.Net;

public static class DisplayFusionFunction
{
    public static void Run(IntPtr windowHandle)
    {
        ExplorerWindows.InitExplorerHandler();
        BFS.Dialog.ShowMessageInfo(ExplorerWindows.GetPath(windowHandle));
      }
}

// The following code is adapted from this solution:
// https://stackoverflow.com/questions/36886355/retrieve-the-full-path-of-an-explorer-window-through-a-handle-in-c-sharp
public static class ExplorerWindows
{
    static Guid CLSID_ShellApplication = new Guid("13709620-C279-11CE-A49E-444553540000");
    static Type shellApplicationType = Type.GetTypeFromCLSID(CLSID_ShellApplication, true);
    static object shellApplication = null;
    static object windows = null;
    static Type windowsType = null;
    static object count = null;
    
    public static void InitExplorerHandler()
    {
        shellApplication = Activator.CreateInstance(shellApplicationType);
        windows = shellApplicationType.InvokeMember("Windows", System.Reflection.BindingFlags.InvokeMethod, null, shellApplication, new object[] { });        
        windowsType = windows.GetType();
        count = windowsType.InvokeMember("Count", System.Reflection.BindingFlags.GetProperty, null, windows, null);
    }
    
    public static string GetPath(IntPtr windowHandle)
    {
        string currDirectory = "";
        Parallel.For(0, (int)count, i =>
        {
            try
            {
                object item = windowsType.InvokeMember("Item", System.Reflection.BindingFlags.InvokeMethod, null, windows, new object[] { i });
                Type itemType = item.GetType();
                string itemName = (string)itemType.InvokeMember("Name", System.Reflection.BindingFlags.GetProperty, null, item, null);
                if (itemName == "Windows Explorer" || itemName == "File Explorer" || itemName == "Explorer")
                {
                    string itemHandle = itemType.InvokeMember("HWND", System.Reflection.BindingFlags.GetProperty, null, item, null).ToString();
                    if (itemHandle == windowHandle.ToString())
                    {
                        currDirectory = (string)itemType.InvokeMember("LocationURL", System.Reflection.BindingFlags.GetProperty, null, item, null);
                    }
                }
            }
            catch (Exception e)
            {

            }
        });
        
        return WebUtility.HtmlDecode(currDirectory);
    }
}
Aug 9, 2021  • #2
Subscribe to this discussion topic using RSS
Was this helpful?  Login to Vote(-)  Login to Vote(-)