且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检查用户对文件共享的读访问权限

更新时间:2023-12-02 14:31:28

嗨  Pallavi Ganesh,

感谢您发布此处。

对于您的问题,您可以尝试以下代码来获取管理员和系统以外的指定目录权限。所有路径都不仅适用于共享文件。

For your question, you could try the code below to get the users of specified directory permissions except Administrators and SYSTEM. All the path would be okay not only for shared files.

 class Program
    {

        static void Main(string[] args)
        {
            GetDirectoryAccountSecurity(@"C:\Users\Desktop\New");
            Console.ReadKey();
        }
        public static List<Account> GetDirectoryAccountSecurity(string DirName)
        {
            List<Account> account = new List<Account>();
            DirectoryInfo dInfo = new DirectoryInfo(DirName);
            if (dInfo.Exists)
            {
                DirectorySecurity sec = Directory.GetAccessControl(DirName, AccessControlSections.All);
                foreach (FileSystemAccessRule rule in sec.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)))
                {
                    if (rule.IdentityReference.Value != @"NT AUTHORITY\SYSTEM" && rule.IdentityReference.Value != @"BUILTIN\Administrators")
                    {
                        Account acc = new Account()
                        {
                            UserName = rule.IdentityReference.Value,
                            Permission = rule.FileSystemRights.ToString()
                        };
                        account.Add(acc);
                    }

                }
            }
            return account;
        }

    }
    public class Account
    {
        public string UserName { get; set; }
        public string Permission { get; set; }
    }

我的共享文件夹

最诚挚的问候,

Wendy