本文整理汇总了C#中FileSystemRights类的典型用法代码示例。如果您正苦于以下问题:C# FileSystemRights类的具体用法?C# FileSystemRights怎么用?C# FileSystemRights使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
FileSystemRights类属于命名空间,在下文中一共展示了FileSystemRights类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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);
}
示例2: FileHasPermission
/// <summary>
/// Test a file for create file access permissions
/// </summary>
///
/// <param name="FilePath">Full path to file</param>
/// <param name="AccessRight">File System right tested</param>
///
/// <returns>State</returns>
public static bool FileHasPermission(string FilePath, FileSystemRights AccessRight)
{
if (string.IsNullOrEmpty(FilePath)) return false;
try
{
AuthorizationRuleCollection rules = File.GetAccessControl(FilePath).GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier));
WindowsIdentity identity = WindowsIdentity.GetCurrent();
foreach (FileSystemAccessRule rule in rules)
{
if (identity.Groups.Contains(rule.IdentityReference))
{
if ((AccessRight & rule.FileSystemRights) == AccessRight)
{
if (rule.AccessControlType == AccessControlType.Allow)
return true;
}
}
}
return false;
}
catch
{
throw;
}
}
示例3: SetAcl
private static void SetAcl(string path, SearchResult user, FileSystemRights right)
{
var userId = user.Properties["userPrincipalName"][0].ToString();
var fullUserName = user.Properties["name"][0].ToString();
var fullPath = path + fullUserName;
var dir = new DirectoryInfo(fullPath);
var ds = new DirectorySecurity();
ds.SetAccessRuleProtection(true, false);
var uacl = new FileSystemAccessRule(userId,
right,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Allow);
ds.AddAccessRule(uacl);
var domainAdmins = new SecurityIdentifier(WellKnownSidType.AccountDomainAdminsSid, AppSettings.GeneralSettings.DomainSid);
var pacl = new FileSystemAccessRule(domainAdmins,
FileSystemRights.FullControl,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Allow);
ds.AddAccessRule(pacl);
var system = new SecurityIdentifier(WellKnownSidType.LocalSystemSid, null);
var sacl = new FileSystemAccessRule(system,
FileSystemRights.FullControl,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Allow);
ds.AddAccessRule(sacl);
dir.SetAccessControl(ds);
}
示例4: AddAclRuleOnDirectory
private static void AddAclRuleOnDirectory(string directory, IdentityReference identity, FileSystemRights rights, AccessControlType type)
{
var acl = Directory.GetAccessControl(directory);
acl.PurgeAccessRules(identity);
acl.AddAccessRule(new FileSystemAccessRule(identity, rights, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, type));
Directory.SetAccessControl(directory, acl);
}
示例5: AddAclRuleOnFile
private static void AddAclRuleOnFile(string file, IdentityReference identity, FileSystemRights rights, AccessControlType type)
{
var acl = File.GetAccessControl(file);
acl.PurgeAccessRules(identity);
acl.AddAccessRule(new FileSystemAccessRule(identity, rights, type));
File.SetAccessControl(file, acl);
}
示例6: HasAccess
public bool HasAccess(FileSystemRights rights)
{
WindowsIdentity user = (WindowsIdentity)principal.Identity;
// Get the collection of authorization rules that apply to the specified directory
AuthorizationRuleCollection acl = directory.GetAccessControl().GetAccessRules(true, true, typeof(SecurityIdentifier));
// These are set to true if either the allow access or deny access rights are set
bool allowAccess = false;
bool denyAccesss = false;
foreach (FileSystemAccessRule currentRule in acl)
{
// If the current rule applies to the current user
if (user.User.Equals(currentRule.IdentityReference) || principal.IsInRole((SecurityIdentifier)currentRule.IdentityReference))
{
if (currentRule.AccessControlType.Equals(AccessControlType.Deny))
{
if ((currentRule.FileSystemRights & rights) == rights)
{
denyAccesss = true;
}
}
else if (currentRule.AccessControlType.Equals(AccessControlType.Allow))
{
if ((currentRule.FileSystemRights & rights) == rights)
{
allowAccess = true;
}
}
}
}
return allowAccess & !denyAccesss;
}
示例7: base
public FileSystemAuditRule
(String identity, FileSystemRights fileSystemRights,
InheritanceFlags inheritanceFlags,
PropagationFlags propagationFlags, AuditFlags auditFlags)
: base(IdentityReference.IdentityFromName(identity),
(int)fileSystemRights, false, inheritanceFlags,
propagationFlags, auditFlags) {}
示例8: AddFileSecurity
public static void AddFileSecurity(string fileName, string WindowsAccount, FileSystemRights rights, AccessControlType accessControlType)
{
FileSecurity fSecurity = File.GetAccessControl(fileName);
fSecurity.AddAccessRule(new FileSystemAccessRule(WindowsAccount, rights, accessControlType));
File.SetAccessControl(fileName, fSecurity);
}
示例9: 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);
}
示例10: base
public FileSystemAccessRule
(String identity, FileSystemRights fileSystemRights,
InheritanceFlags inheritanceFlags,
PropagationFlags propagationFlags, AccessControlType type)
: base(IdentityReference.IdentityFromName(identity),
(int)fileSystemRights, false, inheritanceFlags,
propagationFlags, type) {}
示例11: FileAccessDenier
public FileAccessDenier(FileInfo file, FileSystemRights rights)
{
this.file = file;
this.access = this.file.GetAccessControl();
this.denial = new FileSystemAccessRule(WindowsIdentity.GetCurrent().Name, rights, AccessControlType.Deny);
this.access.AddAccessRule(this.denial);
this.file.SetAccessControl(this.access);
}
示例12: ReplacePermissions
static void ReplacePermissions(string filepath, WellKnownSidType sidType, FileSystemRights allow)
{
FileSecurity sec = File.GetAccessControl(filepath);
SecurityIdentifier sid = new SecurityIdentifier(sidType, null);
sec.PurgeAccessRules(sid); //remove existing
sec.AddAccessRule(new FileSystemAccessRule(sid, allow, AccessControlType.Allow));
File.SetAccessControl(filepath, sec);
}
示例13: AddAccessRule
/// <summary>
/// Add an access rule to a folder
/// </summary>
///
/// <param name="Path">Folder path</param>
/// <param name="User">UNC path to user profile ex. Environment.UserDomainName + "\\" + Environment.UserName</param>
/// <param name="Rights">Desired file system rights</param>
/// <param name="Access">Desired level of access</param>
public static void AddAccessRule(string Path, string User, FileSystemRights Rights, AccessControlType Access)
{
// Get a DirectorySecurity object that represents the current security settings
System.Security.AccessControl.DirectorySecurity sec = System.IO.Directory.GetAccessControl(Path);
// Add the FileSystemAccessRule to the security settings
FileSystemAccessRule accRule = new FileSystemAccessRule(User, Rights, Access);
sec.AddAccessRule(accRule);
}
示例14: HasFilePermissions
private static bool HasFilePermissions(string path, IdentityReference identity, FileSystemRights permissions)
{
var dirInfo = new FileInfo(path);
var dirSecurity = dirInfo.GetAccessControl(AccessControlSections.All);
AuthorizationRuleCollection rules = dirSecurity.GetAccessRules(true, true, typeof(NTAccount));
return HasPermissions(rules, identity, permissions);
}
示例15: CheckAccessRight
public static bool CheckAccessRight(DirectoryInfo directory, FileSystemRights right)
{
var user = WindowsIdentity.GetCurrent();
var p = new WindowsPrincipal(user);
AuthorizationRuleCollection acl =
directory.GetAccessControl().GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier));
return CheckAccessRight(user, p, acl, right);
}