7

Is there a way to open the Advanced Security Settings dialog for a directory/file programmatically? This is the dialog I want to open:

Advanced Security Dialog

You open it by clicking the Advanced button on the Security properties dialog for the directory/file.

Security Properties

There is this answer that shows how to open the Security properties tab using ShellExecuteEx, so perhaps there are different parameters that can be used to open the Advanced Security Settings dialog directly, but I don't know where to find documentation (or where to look in the registry) for the verbs/parameters supported.

There is also the EditSecurityAdvanced API, but it looks like that requires implementing the functionality to get/set the ACL instead of using the functionality built in to the Windows shell.

I'm working in VB.NET, but can translate C# or Windows API calls as needed, and pointers for how to do my own research would also be appreciated.

Community
  • 1
  • 1
Mark
  • 8,140
  • 1
  • 14
  • 29
  • I'm not sure you can do this, the advanced window is likely a child of the main properties window. – DavidG Dec 01 '16 at 16:48
  • 1
    If you don't find a solution to open that dialog directly, perhaps consider opening the normal security dialog and via automation press the "Advanced" button? – Visual Vincent Dec 01 '16 at 16:52
  • 1
    Wow, +1 just for "but can translate C# or Windows API calls as needed, and pointers for how to do my own research would also be appreciated." Around here, one grows tired of programmers who won't take any initiative and are too lazy to so much as run code in their non-native .NET dialect through a conversion utility. – Cody Gray - on strike Dec 02 '16 at 15:25

1 Answers1

2

I couldn't find a direct way of opening it either. Using automation like the commentator Visual Vincent suggested isn't too hard. Don't forget to add references to the assemblies and import System.Windows.Automation

Then this code should press the Advanced button for you. This way still creates the main properties dialog though.

Dim FileName As String = "The file name you are viewing the properties of"
Dim AE As AutomationElement = AutomationElement.RootElement.FindFirst(TreeScope.Children, New PropertyCondition(AutomationElement.NameProperty, FileName + " Properties"))
Dim Advancedbtn = AE.FindFirst(TreeScope.Element Or TreeScope.Descendants, New PropertyCondition(AutomationElement.NameProperty, "Advanced"))
TryCast(Advancedbtn.GetCurrentPattern(InvokePattern.Pattern), InvokePattern).Invoke()
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
Evan P
  • 120
  • 6
  • Thanks - I've never used this API, so this example really helps and works well. I had to insert a wait so that the properties dialog was displayed before this code ran, and I'm wondering if it's not better just to leave it to the user to click advanced, but this gives me an option to try out. – Mark Dec 01 '16 at 22:07
  • Nice answer! I edited it just to improve the readability of the code a bit. – Visual Vincent Dec 01 '16 at 23:19
  • @Mark, It didn't occur to me that a wait was needed, worked without it on my machine. Just luck I guess. – Evan P Dec 02 '16 at 00:27
  • I wonder if using [`SHSetInstanceExplorer()`](https://blogs.msdn.microsoft.com/oldnewthing/20080528-00/?p=22163) might help here, to avoid needing to guess which dialog you are talking about and avoiding accidentally clicking on a dialog you didn't create. You can also probably use UI Automation to subscribe to window events if that doesn't let you know when exactly the window was created. – andlabs Dec 02 '16 at 22:40