Display file Properties > Security > Edit (permissions)
Asked Answered
R

2

6

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?

Regardless answered 27/10, 2022 at 16:59 Comment(2)
There is no Edit.. button in that dialog.Belak
Yes, I added another image. The second one is the result of clicking Edit...Regardless
B
2

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
Billfold answered 4/11, 2022 at 22:47 Comment(0)
C
1

What I know, you need to use EditSecurity function from aclui.dll and implement ISecurityInformation interface.

[DllImport("aclui.dll", CharSet = CharSet.Auto)]
static extern bool EditSecurity(IntPtr hwnd, ISecurityInformation psi);

Interface

[Guid("965FC360-16FF-11d0-91CB-00AA00BBB723")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ISecurityInformation
{
    void GetObjectInformation([Out] out SI_OBJECT_INFO pObjectInfo);
    void GetSecurity([In] uint RequestedInformation, [Out] out IntPtr ppSecurityDescriptor, [In] bool fDefault);
    void SetSecurity([In] uint SecurityInformation, [In] IntPtr pSecurityDescriptor);
    void GetAccessRights([In] IntPtr pguidObjectType, [In] uint dwFlags, [Out] out IntPtr ppAccess, [Out] out int pcAccesses, [Out] out int piDefaultAccess);
    void MapGeneric([In] IntPtr pguidObjectType, [In] IntPtr pAceFlags, [In, Out] ref uint pMask);
    void GetInheritTypes([Out] out IntPtr ppInheritTypes, [Out] out int pcInheritTypes);
    void PropertySheetPageCallback([In] IntPtr hwnd, [In] uint uMsg, [In] uint uPage);
}

SI_OBJECT_INFO structure

[StructLayout(LayoutKind.Sequential)]
public struct SI_OBJECT_INFO
{
    public uint dwFlags;
    public IntPtr hInstance;
    public IntPtr pszServerName;
    public IntPtr pszObjectName;
    public IntPtr pszPageTitle;
    public Guid guidObjectType;
}

SecurityInformation class. Right now, I don't know how to implement all methods of ISecurityInformation.

public class SecurityInformation : ISecurityInformation
{
    SecurityInformation() 
    { 
    }

    void ISecurityInformation.GetObjectInformation(out SI_OBJECT_INFO pObjectInfo)
    {
        pObjectInfo = new SI_OBJECT_INFO()
        {
            // TODO
        };
    }

    void ISecurityInformation.GetSecurity(uint RequestedInformation, out IntPtr ppSecurityDescriptor, bool fDefault)
    {
        // TODO
    }

    void ISecurityInformation.SetSecurity(uint SecurityInformation, IntPtr pSecurityDescriptor)
    {
        // TODO
    }

    void ISecurityInformation.GetAccessRights(IntPtr pguidObjectType, uint dwFlags, out IntPtr ppAccess, out int pcAccesses, out int piDefaultAccess)
    {
        // TODO
    }

    void ISecurityInformation.MapGeneric(IntPtr pguidObjectType, IntPtr pAceFlags, ref uint pMask)
    {
        // TODO
    }

    void ISecurityInformation.GetInheritTypes(out IntPtr ppInheritTypes, out int pcInheritTypes)
    {
        // TODO
    }

    void ISecurityInformation.PropertySheetPageCallback(IntPtr hwnd, uint uMsg, uint uPage)
    {
        // TODO
    }
}
Combinative answered 4/11, 2022 at 15:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.