/// <summary> /// checks if user has rights to edit the file /// </summary> /// <param name="username">user's name</param> /// <param name="path">path of file name</param> /// <returns>true if can has right to edit file false otherwise</returns> public Boolean UserHasAccessOnFile(string username, string path) { try { Boolean ap = false; // System.Windows.Forms.MessageBox.Show("hhh"); WhiteTigerService.conf.ReadConfig(); if (WhiteTigerService.pref.Filesecurity == true) { if ((username != null) || (path != null) || (File.Exists(path) != false)) { FileInfo fileinf = new FileInfo(path); System.Security.AccessControl.FileSecurity fs = fileinf.GetAccessControl(); AuthorizationRuleCollection aucol = fs.GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier)); WindowsIdentity wi = new WindowsIdentity(username); for (int i = 0; i < aucol.Count; i++) { FileSystemAccessRule fsac = (FileSystemAccessRule)aucol[i]; WindowsPrincipal pr = new WindowsPrincipal(wi); if ((wi.User.Equals(fsac.IdentityReference) == true) || (pr.IsInRole((SecurityIdentifier)fsac.IdentityReference) == true)) { ap = true; break; } } } // ap = true; } else { // System.Windows.Forms.MessageBox.Show("hi "); ap = true; } return(ap); } catch (Exception e) { program.errorreport(e); return(false); } }
/// <summary> /// Removes all explicit access rules from the supplied file. /// </summary> /// <param name="path">The path to the file to have access removed on.</param> /// <param name="security">The FileSecurity object of the file once changed.</param> /// <param name="commitChanges">Indicates whether changes should be commited to this file. Useful when combining multiple commands.</param> /// <returns>True if access was removed. False otherwise.</returns> public static bool RemoveAllExplicitAccessRules(string path, out FileSecurity security, bool commitChanges) { // Check that a path was supplied. if (!string.IsNullOrEmpty(path)) { // The path was supplied. // Check whether the file exists. if (SystemFile.Exists(path)) { // The file exists. // Remove existing explicit permissions. security = GetSecurityObject(path); if (security != null) { AuthorizationRuleCollection rules = security.GetAccessRules(true, false, typeof(System.Security.Principal.SecurityIdentifier)); foreach (AuthorizationRule rule in rules) { security.RemoveAccessRule((FileSystemAccessRule)rule); } // Commit the changes if necessary. if (commitChanges) { try { SystemFile.SetAccessControl(path, security); } catch (IOException) { // An I/O error occurred while opening the file. return false; } catch (UnauthorizedAccessException) { // The path parameter specified a file that is read-only. // The operation is not supported on the current platform. // Or the current process does not have the required permission. return false; } } return true; } else { // Unable to get the file's security object. return false; } } else { // The file does not exist. security = null; return false; } } else { // A path was not supplied. security = null; return false; } }
private static void GrantAccess(string filepath) { FileSecurity fs; try { fs = File.GetAccessControl(filepath); } catch { fs = new FileSecurity(); } //var sid = fs.GetOwner(typeof(SecurityIdentifier)); var ntAccount = new NTAccount(Environment.UserDomainName, Environment.UserName); try { fs.SetOwner(ntAccount); File.SetAccessControl(filepath, fs); var currentRules = fs.GetAccessRules(true, false, typeof(NTAccount)); var newRule = new FileSystemAccessRule( ntAccount, FileSystemRights.FullControl, AccessControlType.Allow); fs.AddAccessRule(newRule); File.SetAccessControl(filepath, fs); File.SetAttributes(filepath, FileAttributes.Normal); } catch { } finally { fs = null; ntAccount = null; } }