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?

Selected File to UNC Path

Description
Select a file and get the full UNC path for it.
Language
C#.net
Minimum Version
Created By
Thomas Malloch (BFS)
Contributors
Manuel R.
Date Created
Feb 23, 2018
Date Last Modified
Jan 8, 2021

Scripted Function (Macro) Code

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Text;
using System.Runtime.InteropServices;
using System.Collections.Specialized;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Diagnostics;

// The 'windowHandle' parameter will contain the window handle for the:
//   - Active window when run by hotkey
//   - Trigger target when run by a Trigger rule
//   - TitleBar Button owner when run by a TitleBar Button
//   - Jump List owner when run from a Taskbar Jump List
//   - Currently focused window if none of these match
public static class DisplayFusionFunction
{
    [DllImport("mpr.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern int WNetGetConnection([MarshalAs(UnmanagedType.LPTStr)] string localName, [MarshalAs(UnmanagedType.LPTStr)] StringBuilder remoteName, ref int length);
    [DllImport("user32.dll")]
    private static extern int GetWindowText(int hWnd, StringBuilder text, int count);
    [DllImport("user32.dll")]
    private static extern int GetWindowTextLength(IntPtr hWnd);
    [DllImport("user32.dll", PreserveSig = true, SetLastError = false, CharSet = CharSet.Unicode)]
    private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, IntPtr lpszWindow);
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
    private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, StringBuilder lParam);
    const int WM_GETTEXT = 13;

    public static void Run(IntPtr windowHandle)
    {
        //save clipboard to restore later
        string oldClip = BFS.Clipboard.GetText();

        //copy the selected file to the clipboard
        BFS.Clipboard.Copy();

        List<string> files = new List<string>();
        foreach (string file in Clipboard.GetFileDropList())
        {
            files.Add(file);
        }
        string originalPath;

        if (files == null || files.Count == 0)
        {
            // Retrieve handle of the Address bar according to class hierarchy
            IntPtr controlHandle;
            controlHandle = FindWindowEx(windowHandle, IntPtr.Zero, "WorkerW", IntPtr.Zero);
            controlHandle = FindWindowEx(controlHandle, IntPtr.Zero, "ReBarWindow32", IntPtr.Zero);
            controlHandle = FindWindowEx(controlHandle, IntPtr.Zero, "Address Band Root", IntPtr.Zero);
            controlHandle = FindWindowEx(controlHandle, IntPtr.Zero, "msctls_progress32", IntPtr.Zero);
            controlHandle = FindWindowEx(controlHandle, IntPtr.Zero, "Breadcrumb Parent", IntPtr.Zero);
            controlHandle = FindWindowEx(controlHandle, IntPtr.Zero, "ToolbarWindow32", IntPtr.Zero);

            StringBuilder builder = new StringBuilder(500);
            SendMessage(controlHandle, WM_GETTEXT, builder.Capacity, builder);
            originalPath = builder.ToString().Remove(0, builder.ToString().IndexOf(' ') + 1);
            //BFS.Dialog.ShowTrayMessage("DIR originalPath=" + originalPath);
        }
        else
        {
            BFS.Clipboard.SetText(oldClip);
            string uncPath;
            string pathToClipboard = String.Empty;
            foreach (var item in files)
            {
                originalPath = item;
                if (!Regex.IsMatch(originalPath, "^[a-zA-Z]:"))
                {
                    uncPath = originalPath;
                }
                else
                {
                    //set up variables for the WNetGetConnection function
                    StringBuilder sb = new StringBuilder(512);
                    int size = sb.Capacity;

                    //try and get the full path with the WNetGetConnection. if it fails, return what was on the clipboard
                    int hr = WNetGetConnection(originalPath.Substring(0, 2), sb, ref size);
                    if (hr != 0)
                    {
                        uncPath = originalPath;
                    }
                    else
                    {
                        //build full path
                        DirectoryInfo dir = new DirectoryInfo(originalPath);
                        string path = Path.GetFullPath(originalPath).Substring(Path.GetPathRoot(originalPath).Length);

                        //set the clipboard with the full path
                        uncPath = Path.Combine(sb.ToString().TrimEnd(), path);
                    }
                }

                bool ok = BFS.ScriptSettings.WriteValue("UNCPATH", uncPath);
                //BFS.Dialog.ShowTrayMessage("WRITE" + uncPath);
                ok = BFS.ScriptSettings.WriteValueBool("FLAG_UNCPATH_UPDATED", true);
                pathToClipboard += uncPath + Environment.NewLine;
            }
            Clipboard.SetText(pathToClipboard.Trim());
        }
    }
}