Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6809123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:02:53+00:00 2026-05-26T20:02:53+00:00

I have the following code: DirectoryInfo directory = new DirectoryInfo(@"C:\Program Files\Company\Product"); if (!directory.Exists) {

  • 0

I have the following code:

DirectoryInfo directory = new DirectoryInfo(@"C:\Program Files\Company\Product");
if (!directory.Exists) { directory.Create(); }

DirectorySecurity directorySecurity = directory.GetAccessControl();
SecurityIdentifier securityIdentifier = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
directorySecurity.AddAccessRule(
    new FileSystemAccessRule(
        securityIdentifier,
        FileSystemRights.Write,
        InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
        PropagationFlags.None,
        AccessControlType.Allow));
directory.SetAccessControl(directorySecurity);

The call to AddAccessRule throws an InvalidOperationException with the following stack trace:

System.InvalidOperationException: This access control list is not in canonical form and therefore cannot be modified.
   at System.Security.AccessControl.CommonAcl.ThrowIfNotCanonical()
   at System.Security.AccessControl.CommonAcl.AddQualifiedAce(SecurityIdentifier sid, AceQualifier qualifier, Int32 accessMask, AceFlags flags, ObjectAceFlags objectFlags, Guid objectType, Guid inheritedObjectType)
   at System.Security.AccessControl.DiscretionaryAcl.AddAccess(AccessControlType accessType, SecurityIdentifier sid, Int32 accessMask, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags)
   at System.Security.AccessControl.CommonObjectSecurity.ModifyAccess(AccessControlModification modification, AccessRule rule, Boolean& modified)
   at System.Security.AccessControl.CommonObjectSecurity.AddAccessRule(AccessRule rule)
   at System.Security.AccessControl.FileSystemSecurity.AddAccessRule(FileSystemAccessRule rule)

This only happens on some systems (I’ve seen Windows XP and Windows 7). In the situations where the error occurs, viewing the security permissions for the directory using Windows Explorer usually causes a message box to be shown with the following text:

The permissions on are incorrectly ordered, which may
cause some entries to be ineffective. Press OK to continue and sort
the permissions correctly, or Cancel to reset the permissions.

Clicking OK at this point fixes the problem. What’s going on here? How does a system get into this state, and is there any way to detect/fix it programmatically (i.e. without having the user manually use Explorer to fix this)?

Update

I did a bit more research about ACL, what canonical form is, and why it’s necessary. I’m still not sure how a file would normally get into this state, but I found that the Icacls tool can be used to create a directory with a non-canonical ACL by saving the permission list, altering it to be out-of-order, and restoring it. Now I just need a way to fix it without requiring user interaction.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:02:54+00:00Added an answer on May 26, 2026 at 8:02 pm

    I found the solution to this in an MSDN blog post: Say wwhhhaaaat? – The access control list is not canonical. Basically, you need to construct a new DACL with the same permissions, but in the correct canonical order:

    static void Main(string[] args)
    {
        // directory with known ACL problem (created using Icacls)
        DirectoryInfo directoryInfo = new DirectoryInfo("acltest");
    
        var directorySecurity = directoryInfo.GetAccessControl(AccessControlSections.Access);
        CanonicalizeDacl(directorySecurity);
        directoryInfo.SetAccessControl(directorySecurity);
    }
    
    static void CanonicalizeDacl(NativeObjectSecurity objectSecurity)
    {
        if (objectSecurity == null) { throw new ArgumentNullException("objectSecurity"); }
        if (objectSecurity.AreAccessRulesCanonical) { return; }
    
        // A canonical ACL must have ACES sorted according to the following order:
        //   1. Access-denied on the object
        //   2. Access-denied on a child or property
        //   3. Access-allowed on the object
        //   4. Access-allowed on a child or property
        //   5. All inherited ACEs 
        RawSecurityDescriptor descriptor = new RawSecurityDescriptor(objectSecurity.GetSecurityDescriptorSddlForm(AccessControlSections.Access));
    
        List<CommonAce> implicitDenyDacl = new List<CommonAce>();
        List<CommonAce> implicitDenyObjectDacl = new List<CommonAce>();
        List<CommonAce> inheritedDacl = new List<CommonAce>();
        List<CommonAce> implicitAllowDacl = new List<CommonAce>();
        List<CommonAce> implicitAllowObjectDacl = new List<CommonAce>();
    
        foreach (CommonAce ace in descriptor.DiscretionaryAcl)
        {
            if ((ace.AceFlags & AceFlags.Inherited) == AceFlags.Inherited) { inheritedDacl.Add(ace); }
            else
            {
                switch (ace.AceType)
                {
                    case AceType.AccessAllowed:
                        implicitAllowDacl.Add(ace);
                        break;
    
                    case AceType.AccessDenied:
                        implicitDenyDacl.Add(ace);
                        break;
    
                    case AceType.AccessAllowedObject:
                        implicitAllowObjectDacl.Add(ace);
                        break;
    
                    case AceType.AccessDeniedObject:
                        implicitDenyObjectDacl.Add(ace);
                        break;
                }
            }
        }
    
        Int32 aceIndex = 0;
        RawAcl newDacl = new RawAcl(descriptor.DiscretionaryAcl.Revision, descriptor.DiscretionaryAcl.Count);
        implicitDenyDacl.ForEach(x => newDacl.InsertAce(aceIndex++, x));
        implicitDenyObjectDacl.ForEach(x => newDacl.InsertAce(aceIndex++, x));
        implicitAllowDacl.ForEach(x => newDacl.InsertAce(aceIndex++, x));
        implicitAllowObjectDacl.ForEach(x => newDacl.InsertAce(aceIndex++, x));
        inheritedDacl.ForEach(x => newDacl.InsertAce(aceIndex++, x));
    
        if (aceIndex != descriptor.DiscretionaryAcl.Count)
        {
            System.Diagnostics.Debug.Fail("The DACL cannot be canonicalized since it would potentially result in a loss of information");
            return;
        }
    
        descriptor.DiscretionaryAcl = newDacl;
        objectSecurity.SetSecurityDescriptorSddlForm(descriptor.GetSddlForm(AccessControlSections.Access), AccessControlSections.Access);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple application with the following code: FileInfo[] files = (new DirectoryInfo(initialDirectory)).GetFiles();
I have the following code DirectoryInfo taskDirectory = new DirectoryInfo(this.taskDirectoryPath); FileInfo[] taskFiles = taskDirectory.GetFiles(*
I have following code class User attr_accessor :name end u = User.new u.name =
I have following code snipped: ... var tpc = new ThirtPartyClass(); tpc.ExecuteCommand(); tpc.ExecuteCommand(); ...
I have the following code I am trying to fix foreach (System.IO.DirectoryInfo dir in
I have following code in C# PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations);
I have following code snippet in c#. var list = new List<string> { a,
I have following code to play small audio files private void playVoice() { if
I have following code that I creating a grid var store = Ext.create('Ext.data.ArrayStore', {
I have following code for opening files programatically: Uri path = Uri.fromFile(file); Intent intent

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.