2015-07-03 64 views
0
private bool GrantAccess(string fullPath) 
{ 
     DirectoryInfo dInfo = new DirectoryInfo(fullPath); 
     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); 
     return true; 
} 

我正在使用这种方法删除被拒绝的权限,但它不能正常工作,请帮助解决此问题。我如何删除拒绝文件夹的权限?

+0

你应该运行你的应用程序作为管理员!如果您正在调试,只需以管理员身份运行Visual Studio。 –

回答

0
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); 

     } 
    } 
} 

更多信息这里: https://msdn.microsoft.com/en-us/library/system.io.directory.setaccesscontrol(v=vs.110).aspx

确保您的应用程序运行在较高的特权