jewalky

Untitled

Mar 14th, 2017
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.88 KB | None | 0 0
  1.         //mxd
  2.         public static bool CheckWritePremissions(string path)
  3.         {
  4.             try
  5.             {
  6.                 DirectoryInfo di = new DirectoryInfo(path);
  7.                 DirectorySecurity ds = di.GetAccessControl();
  8.                 AuthorizationRuleCollection rules = ds.GetAccessRules(true, true, typeof(NTAccount));
  9.                 WindowsIdentity currentuser = WindowsIdentity.GetCurrent();
  10.  
  11.                 if(currentuser != null)
  12.                 {
  13.                     WindowsPrincipal principal = new WindowsPrincipal(currentuser);
  14.                     foreach(AuthorizationRule rule in rules)
  15.                     {
  16.                         FileSystemAccessRule fsar = rule as FileSystemAccessRule;
  17.                         if(fsar != null && (fsar.FileSystemRights & FileSystemRights.WriteData) > 0)
  18.                         {
  19.                             NTAccount account = rule.IdentityReference as NTAccount;
  20.                             if(account != null && principal.IsInRole(account.Value)) return true;
  21.                         }
  22.                     }
  23.                 }
  24.             }
  25.             catch(UnauthorizedAccessException) { }
  26.  
  27.             return false;
  28.         }
Add Comment
Please, Sign In to add comment