當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。