Установка разрешений NTFS в C # .NET - PullRequest
5 голосов
/ 17 сентября 2011

Как мне установить разрешения NTFS в C # .NET? Я пытаюсь изменить разрешения на чтение / запись в .NET. Я новичок, пожалуйста, помогите!

1 Ответ

8 голосов
/ 17 сентября 2011

вы должны быть в состоянии сделать это с Пространство имен System.Security.AccessControl.

System.Security.AccessControl;


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, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None,
    ControlType));


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


 }

Пример вызова:

//Get current user
string user = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
//Deny writing to the file
AddDirectorySecurity(@"C:\Users\Phil\Desktop\hello.ini",user, FileSystemRights.Write, AccessControlType.Deny);
...