当前位置: 首页>>代码示例>>C#>>正文


C# DirectoryInfo.SetAccessControl方法代码示例

本文整理汇总了C#中System.IO.DirectoryInfo.SetAccessControl方法的典型用法代码示例。如果您正苦于以下问题:C# DirectoryInfo.SetAccessControl方法的具体用法?C# DirectoryInfo.SetAccessControl怎么用?C# DirectoryInfo.SetAccessControl使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.IO.DirectoryInfo的用法示例。


在下文中一共展示了DirectoryInfo.SetAccessControl方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TakeOwnership

 public static void TakeOwnership(string FD)
 {
     try
     {
         var myProcToken = new AccessTokenProcess(Process.GetCurrentProcess().Id, TokenAccessType.TOKEN_ALL_ACCESS | TokenAccessType.TOKEN_ADJUST_PRIVILEGES);
         myProcToken.EnablePrivilege(new Microsoft.Win32.Security.TokenPrivilege(Microsoft.Win32.Security.TokenPrivilege.SE_TAKE_OWNERSHIP_NAME, true));
         SecurityIdentifier identifier = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null);
         NTAccount identity = (NTAccount)identifier.Translate(typeof(NTAccount));
         if (File.Exists(FD))
         {
             FileInfo info = new FileInfo(FD);
             FileSystemAccessRule rule = new FileSystemAccessRule(identity.Value, FileSystemRights.FullControl, AccessControlType.Allow);
             FileSecurity accessControl = info.GetAccessControl(AccessControlSections.Owner);
             accessControl.SetOwner(new NTAccount(identity.Value));
             info.SetAccessControl(accessControl);
             accessControl.AddAccessRule(rule);
             info.SetAccessControl(accessControl);
         }
         if (Directory.Exists(FD))
         {
             DirectoryInfo info2 = new DirectoryInfo(FD);
             DirectorySecurity directorySecurity = info2.GetAccessControl(AccessControlSections.All);
             directorySecurity.SetOwner(identity);
             info2.SetAccessControl(directorySecurity);
             directorySecurity.AddAccessRule(new FileSystemAccessRule(identity, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow));
             info2.SetAccessControl(directorySecurity);
         }
         Clear(FD);
     }
     catch (Exception)
     {
     }
 }
开发者ID:vascofo,项目名称:Windows-10-Login-Background-Changer,代码行数:33,代码来源:Class1.cs

示例2: ProtectFolder

        public static void ProtectFolder(string folderToProtect, string accountFullAccessAllowed, string accountDenied, bool allowDeniedToList)
        {
            EnsureFolderExists(folderToProtect);

            var directoryInfo = new DirectoryInfo(folderToProtect);

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

            // Apply the account has FULL access to this folder
            directorySecurity.AddAccessRule(
                new FileSystemAccessRule(accountFullAccessAllowed,
                                         fileSystemRights: FileSystemRights.FullControl,
                                         inheritanceFlags:
                                             InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
                                         propagationFlags: PropagationFlags.None,
                                         type: AccessControlType.Allow)
                );

            if (!string.IsNullOrEmpty(accountDenied))
            {
                // Other users can only list files
                if (allowDeniedToList)
                {
                    directorySecurity.AddAccessRule(
                        new FileSystemAccessRule(accountDenied, // Instead of just AccountUserNameWizUser
                                                 fileSystemRights: FileSystemRights.ListDirectory,
                                                 inheritanceFlags:
                                                     InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
                                                 propagationFlags: PropagationFlags.None,
                                                 type: AccessControlType.Allow)
                        );
                }

                directorySecurity.AddAccessRule(
                    new FileSystemAccessRule(accountDenied,
                                             fileSystemRights:
                                                 FileSystemRights.Delete |
                                                 FileSystemRights.CreateFiles |
                                                 FileSystemRights.CreateDirectories |
                                                 FileSystemRights.AppendData |
                                                 FileSystemRights.ReadExtendedAttributes |
                                                 FileSystemRights.WriteExtendedAttributes |
                                                 FileSystemRights.ExecuteFile |
                    //                                         FileSystemRights.Traverse |
                                                 FileSystemRights.DeleteSubdirectoriesAndFiles |
                                                 FileSystemRights.WriteAttributes |
                                                 FileSystemRights.ChangePermissions |
                                                 FileSystemRights.TakeOwnership,

                                             inheritanceFlags:
                                                 InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
                                             propagationFlags: PropagationFlags.None,
                                             type: AccessControlType.Deny)
                    );
            }

            // Set the new access settings.
            directoryInfo.SetAccessControl(directorySecurity);
        }
开发者ID:nickvil,项目名称:FolderPoll,代码行数:60,代码来源:ProtectFolderHelper.cs

示例3: Apply

        public override void Apply(Prison prison)
        {
            if (prison == null)
            {
                throw new ArgumentNullException("prison");
            }

            WindowsUsersAndGroups.AddUserToGroup(prison.User.UserName, prisonRestrictionsGroup);

            if (Directory.Exists(prison.PrisonHomePath))
            {
                prison.User.Profile.UnloadUserProfileUntilReleased();
                Directory.Delete(prison.PrisonHomePath, true);
            }

            Directory.CreateDirectory(prison.PrisonHomePath);

            DirectoryInfo deploymentDirInfo = new DirectoryInfo(prison.PrisonHomePath);
            DirectorySecurity deploymentDirSecurity = deploymentDirInfo.GetAccessControl();

            // Owner is important to account for disk quota
            SetDirectoryOwner(deploymentDirSecurity, prison);

            // Taking ownership of a file has to be executed with restore privilege enabled
            using (new ProcessPrivileges.PrivilegeEnabler(Process.GetCurrentProcess(), ProcessPrivileges.Privilege.Restore))
            {
                deploymentDirInfo.SetAccessControl(deploymentDirSecurity);
            }
        }
开发者ID:datatonic,项目名称:cf-windows-prison,代码行数:29,代码来源:Filesystem.cs

示例4: FileSystem

        /// <summary>
        /// Initializes a new instance of the <see cref="FileSystem" /> class.
        /// </summary>
        /// <param name="root">The absolute path to the root directory of this file system (..../Pie/ or .../Pie).</param>
        public FileSystem(String root)
        {
            // Append separator to root, if it hasn't got it
            _fileSystemRoot = root + (root.EndsWith(PathSeparator) ? String.Empty : PathSeparator);

            DirectoryInfo dirInfo = new DirectoryInfo(_fileSystemRoot);

            // Create directory if it doesn't exist
            if (!Directory.Exists(_fileSystemRoot))
            {
                Directory.CreateDirectory(_fileSystemRoot);
            }

            // Make sure the directory has the right permissions
            try
            {
                // Attempt to get a list of security permissions from the folder.
                // This will raise an exception if the path is read only or do not have access to view the permissions.
                System.Security.AccessControl.DirectorySecurity ds = Directory.GetAccessControl(_fileSystemRoot);
            }
            catch (UnauthorizedAccessException)
            {
                var security = new DirectorySecurity();
                var windowsIdentity = WindowsIdentity.GetCurrent();
                if (windowsIdentity != null)
                {
                    var id = windowsIdentity.User;
                    var rule = new FileSystemAccessRule(id, FileSystemRights.FullControl, AccessControlType.Allow);
                    security.AddAccessRule(rule);
                    dirInfo.SetAccessControl(security);
                }
            }
        }
开发者ID:Yndal,项目名称:BDSA-Project-2012,代码行数:37,代码来源:FileSystem.cs

示例5: RemoveInheritablePermissons

		private static void RemoveInheritablePermissons(string folderName)
		{
			DirectoryInfo directoryInfo = new DirectoryInfo(folderName);
			DirectorySecurity accessControl = directoryInfo.GetAccessControl();
			accessControl.SetAccessRuleProtection(true, false);
			directoryInfo.SetAccessControl(accessControl);
		}
开发者ID:nickchal,项目名称:pash,代码行数:7,代码来源:InstanceStorePermission.cs

示例6: AddDirectorySecurity

		private static void AddDirectorySecurity(string folderName, string account, FileSystemRights rights, InheritanceFlags inheritance, PropagationFlags propogation, AccessControlType controlType)
		{
			DirectoryInfo directoryInfo = new DirectoryInfo(folderName);
			DirectorySecurity accessControl = directoryInfo.GetAccessControl();
			accessControl.AddAccessRule(new FileSystemAccessRule(account, rights, inheritance, propogation, controlType));
			directoryInfo.SetAccessControl(accessControl);
		}
开发者ID:nickchal,项目名称:pash,代码行数:7,代码来源:InstanceStorePermission.cs

示例7: Add

 public static void Add(string Path, string UserName, FileSystemRights Role)
 {
     DirectoryInfo dirinfo = new DirectoryInfo(Path);
     DirectorySecurity sec = dirinfo.GetAccessControl();
     sec.AddAccessRule(new FileSystemAccessRule(UserName, Role, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
     dirinfo.SetAccessControl(sec);
 }
开发者ID:henrydem,项目名称:yongfa365doc,代码行数:7,代码来源:NTFS.cs

示例8: RemoveDenyDelete

 public static void RemoveDenyDelete()
 {
     var info = new DirectoryInfo( SpazFile.SaveFolder );
     var sec = info.GetAccessControl();
     sec.RemoveAccessRuleSpecific( MakeNewDenyDeleteRule() );
     info.SetAccessControl( sec );
 }
开发者ID:Zazcallabah,项目名称:FixSpaz,代码行数:7,代码来源:Permissions.cs

示例9: GrantModifyAccessToFolder

        public static bool GrantModifyAccessToFolder(string windowsAccountUserName,
            string folderName)
        {
            DirectoryInfo directory = null;
            DirectorySecurity directorySecurity = null;
            FileSystemAccessRule rule = null;

            try
            {

                if (windowsAccountUserName.Length < 1) { return false; }
                if (folderName.Length < 1) { return false; }
                if (!Directory.Exists(folderName)) { return false; }

                directory = new DirectoryInfo(folderName);

                directorySecurity = directory.GetAccessControl();

                rule = new FileSystemAccessRule(windowsAccountUserName,
                                                FileSystemRights.Modify,
                                                InheritanceFlags.None |
                                                InheritanceFlags.ContainerInherit |
                                                InheritanceFlags.ObjectInherit,
                                                PropagationFlags.None,
                                                AccessControlType.Allow);

                directorySecurity.SetAccessRule(rule);

                directory.SetAccessControl(directorySecurity);

                return true;

            }
            catch (Exception) { throw; }
        }
开发者ID:roboshepherd,项目名称:myro-epuck,代码行数:35,代码来源:Installer1.cs

示例10: SetRightAction

        public static ActionResult SetRightAction(Session session)
        {
            try
            {
                string folder = Path.Combine(
                    Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "digiCamControl");
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }
                DirectoryInfo dInfo = new DirectoryInfo(folder);
                DirectorySecurity dSecurity = dInfo.GetAccessControl();
                SecurityIdentifier everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
                dSecurity.AddAccessRule(new FileSystemAccessRule(everyone, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Allow));
                dInfo.SetAccessControl(dSecurity);
                string cachfolder = Path.Combine(
                                    Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "digiCamControl", "Cache");
                if (Directory.Exists(cachfolder))
                {
                    Directory.Delete(cachfolder, true);
                }

            }
            catch (Exception ex)
            {
                session.Log("Set right error " + ex.Message);
            }
            return ActionResult.Success;
        }
开发者ID:Drikus,项目名称:digiCamControl,代码行数:29,代码来源:setup.cs

示例11: ElevatePermissions

 public void ElevatePermissions(string directory)
 {
     var account = new NTAccount(WindowsIdentity.GetCurrent().Name);
     var existingDirectory = new DirectoryInfo(directory);
     var existingDirectorySecurity = existingDirectory.GetAccessControl();
     existingDirectorySecurity.SetOwner(account);
     existingDirectory.SetAccessControl(existingDirectorySecurity);
 }
开发者ID:modulexcite,项目名称:mini-web-deploy,代码行数:8,代码来源:DirectoryWrapper.cs

示例12: UpdateDirectorySecurity

 /// <summary>
 /// Opens up directory access for Everyone at FullAccess.
 /// </summary>
 /// <param name="path">Directory to updated.</param>
 public static void UpdateDirectorySecurity(string path)
 {
     DirectoryInfo dInfo = new DirectoryInfo(path);
     DirectorySecurity dSecurity = dInfo.GetAccessControl();
     dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null),
                             FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow));
     dInfo.SetAccessControl(dSecurity);
 }
开发者ID:Gurux,项目名称:Gurux.DLMS.Net,代码行数:12,代码来源:GXFileInfo.cs

示例13: AddFullAccessToDirectory

		private static void AddFullAccessToDirectory(string user, string directoryPath)
		{
			DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath);
			DirectorySecurity accessControl = directoryInfo.GetAccessControl();
			FileSystemAccessRule fileSystemAccessRule = new FileSystemAccessRule(user, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow);
			accessControl.AddAccessRule(fileSystemAccessRule);
			directoryInfo.SetAccessControl(accessControl);
		}
开发者ID:nickchal,项目名称:pash,代码行数:8,代码来源:ScheduledJobStore.cs

示例14: OpenPermissions

        public static void OpenPermissions(string folder = "") {
            try {
                bool isModified = false;
                DirectoryInfo dirInfo = new DirectoryInfo(HttpContext.Current.Server.MapPath(folder));
                DirectorySecurity dirSec = dirInfo.GetAccessControl();

                AccessRule rule = new FileSystemAccessRule("Users", FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow);
                dirSec.ModifyAccessRule(AccessControlModification.Add, rule, out isModified);
                dirInfo.SetAccessControl(dirSec);

                rule = new FileSystemAccessRule("IIS_IUSRS", FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow);
                dirSec.ModifyAccessRule(AccessControlModification.Add, rule, out isModified);
                dirInfo.SetAccessControl(dirSec);
            } catch (Exception e) {
                string err = e.Message;
            }
        }
开发者ID:janiukjf,项目名称:CURTeCommerce,代码行数:17,代码来源:UDF.cs

示例15: SetFolderACL

 public static bool SetFolderACL(String FolderPath, String UserName, FileSystemRights Rights, AccessControlType AllowOrDeny, InheritanceFlags Inherits, PropagationFlags PropagateToChildren, AccessControlModification AddResetOrRemove)
 {
     bool ret;
     DirectoryInfo folder = new DirectoryInfo(FolderPath);
     DirectorySecurity dSecurity = folder.GetAccessControl(AccessControlSections.All);
     FileSystemAccessRule accRule = new FileSystemAccessRule(UserName, Rights, Inherits, PropagateToChildren, AllowOrDeny); dSecurity.ModifyAccessRule(AddResetOrRemove, accRule, out ret);
     folder.SetAccessControl(dSecurity);
     return ret;
 }
开发者ID:AllanHao,项目名称:MotivationManager,代码行数:9,代码来源:Program.cs


注:本文中的System.IO.DirectoryInfo.SetAccessControl方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。