Search code examples
c#shellexecute

Display file Properties > Security > Edit (permissions)


I successfully implemented the solution from How does one invoke the Windows Permissions dialog programmatically? and related: How to display file properties dialog security tab from c#.

    public static bool ShowFileProperties(string Filename)
    {
        SHELLEXECUTEINFO info = new SHELLEXECUTEINFO();
        info.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(info);
        info.lpVerb = "properties";
        info.lpFile = Filename;
        info.lpParameters = "Security";
        info.nShow = SW_SHOW;
        info.fMask = SEE_MASK_INVOKEIDLIST;
        return ShellExecuteEx(ref info);
    }

However, I would like to get to the Permissions window as you would with the Edit.. button from that dialogue.

enter image description here ![enter image description here

Any ideas?


Solution

  • You can click the button "Edit..." programmatically:

    Edited example from here, matching to the window title seen in your Screenshot:

    private void clickEdit()
    {
        // Look for a top-level window/application called "SupportAssist Properties"
        var root = AutomationElement.RootElement;
        var condition1 = new PropertyCondition(AutomationElement.NameProperty, "SupportAssist Properties");
    
        var treeWalker = new TreeWalker(condition1);
        AutomationElement element = treeWalker.GetFirstChild(root);
    
        if (element == null)
        {
            // not found
            return;
        }
    
        // great!  window found
        var window = element;
    
        // now look for a button with the text "Edit..."
        var buttonElement = window.FindFirst(TreeScope.Descendants,
            new PropertyCondition(AutomationElement.NameProperty, "Edit.."));
        if (buttonElement == null)
        {
            // not found
            return;
        }
    
        // great! now click the button
        var invokePattern = buttonElement.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
        invokePattern?.Invoke();
    }
    

    Important! The linked source of this code says the following:

    var buttonElement = window.FindFirst(TreeScope.Children
    

    But this does not work in the "Properties" window. It does not find "Edit..." in this level. You have to search for it one level deeper:

    var buttonElement = window.FindFirst(TreeScope.Descendants