• C#如何判断某个文件夹是否为共享,访问权限为只读


    List<FileSystemRights> ret = new List<FileSystemRights>();

                DirectorySecurity dirSec = Directory.GetAccessControl(@"c:\test", AccessControlSections.All);
                AuthorizationRuleCollection rules = dirSec.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount));
                foreach (FileSystemAccessRule rule in rules)
                {
                    ret.Add(rule.FileSystemRights);
                }

    ret里面就是各个帐号对该目录的访问权限,你在里面判断一下就知道了,格式是这样的:
    ReadData | WriteData | AppendData | ReadExtendedAttributes | WriteExtendedAttributes | ExecuteFile | DeleteSubdirectoriesAndFiles | ReadAttributes | WriteAttributes | Delete | ReadPermissions | ChangePermissions | TakeOwnership | Synchronize

     **********************************

    判断在该文件夹下面有没有创建文件的权限

    using System;
    using System.IO;
    using System.Security.AccessControl;

    namespace FileSystemExample
    {
        class DirectoryExample
        {
            public static void Main()
            {
                try
                {
                    string DirectoryName = "TestDirectory";

                    Console.WriteLine("Adding access control entry for " + DirectoryName);

                    // Add the access control entry to the directory.
                    AddDirectorySecurity(DirectoryName, @"MYDOMAIN\MyAccount", FileSystemRights.ReadData, AccessControlType.Allow);

                    Console.WriteLine("Removing access control entry from " + DirectoryName);

                    // Remove the access control entry from the directory.
                    RemoveDirectorySecurity(DirectoryName, @"MYDOMAIN\MyAccount", FileSystemRights.ReadData, AccessControlType.Allow);

                    Console.WriteLine("Done.");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }

                Console.ReadLine();
            }

            // Adds an ACL entry on the specified directory for the specified account.
            public static void AddDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
            {
                // Create a new DirectoryInfo object.
                DirectoryInfo dInfo = new DirectoryInfo(FileName);

                // Get a DirectorySecurity object that represents the
                // current security settings.
                DirectorySecurity dSecurity = dInfo.GetAccessControl();

                // Add the FileSystemAccessRule to the security settings.
                dSecurity.AddAccessRule(new FileSystemAccessRule(Account,
                                                                Rights,
                                                                ControlType));

                // Set the new access settings.
                dInfo.SetAccessControl(dSecurity);

            }

            // Removes an ACL entry on the specified directory for the specified account.
            public static void RemoveDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
            {
                // Create a new DirectoryInfo object.
                DirectoryInfo dInfo = new DirectoryInfo(FileName);

                // Get a DirectorySecurity object that represents the
                // current security settings.
                DirectorySecurity dSecurity = dInfo.GetAccessControl();

                // Add the FileSystemAccessRule to the security settings.
                dSecurity.RemoveAccessRule(new FileSystemAccessRule(Account,
                                                                Rights,
                                                                ControlType));

                // Set the new access settings.
                dInfo.SetAccessControl(dSecurity);

            }
        }
    }

  • 相关阅读:
    【python进阶】哈希算法(Hash)
    【数据库】MongoDB操作命令
    【数据库】MongoDB安装&配置
    【python基础】元组方法汇总
    【python基础】集合方法汇总
    滴水穿石-04Eclipse中常用的快捷键
    滴水穿石-03一道面试题引发的思考
    滴水穿石-02制作说明文档
    滴水穿石-01JAVA和C#的区别
    步步为营101-同一个PCode下重复的OrderNumber重新排序
  • 原文地址:https://www.cnblogs.com/hxwzwiy/p/2412287.html
Copyright © 2020-2023  润新知