• How to check Read or Write permissions on a folder in C#?

    Posted on May 31, 2012 by in Dotnet Tips & Tricks, Tips & Tricks

    using System.Security;
    using System.Security.Permissions;
    using System.Security.AccessControl;
    using System.IO;
    public static bool CheckFolderPermissions(string directoryPath, FileSystemRights accessType)
            {
                bool hasAccess = true;
                try
                {
                    AuthorizationRuleCollection collection = Directory.
                                                GetAccessControl(directoryPath)
                                                .GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount));
                    foreach (FileSystemAccessRule rule in collection)
                    {
                        if ((rule.FileSystemRights & accessType) > 0) 
                        {
                            return hasAccess;
                        }                    
                    }
                }            
                catch (Exception ex)
                {
                    hasAccess = false;                
                }
                return hasAccess;
            }

    Example:

    CheckFolderPermissions(folderPath,                            System.Security.AccessControl.FileSystemRights.WriteData)
    Be Sociable, Share!
      Post Tagged with

    Written by

    View all articles by

    Email : [email protected]

    One Responseso far.

    1. Duy Nguyen Thanh says:

      It’s work fine. Thanks.

    Leave a Reply