本文整理汇总了C#中System.Security.AccessControl.FileSecurity.SetAccessRuleProtection方法的典型用法代码示例。如果您正苦于以下问题:C# FileSecurity.SetAccessRuleProtection方法的具体用法?C# FileSecurity.SetAccessRuleProtection怎么用?C# FileSecurity.SetAccessRuleProtection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.AccessControl.FileSecurity
的用法示例。
在下文中一共展示了FileSecurity.SetAccessRuleProtection方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetSecuritySettings
static private FileSecurity GetSecuritySettings() {
FileSecurity security = new FileSecurity();
security.SetAccessRuleProtection(true, false);
security.AddAccessRule(
(FileSystemAccessRule) security.AccessRuleFactory(
new NTAccount(
WindowsIdentity.GetCurrent().Name),
// Full control
-1,
false,
InheritanceFlags.None,
PropagationFlags.None,
AccessControlType.Allow));
return security;
}
示例2: GetSecureFileStream
public static FileStream GetSecureFileStream(string path, int bufferSize, FileOptions options)
{
if (path == null)
throw new ArgumentNullException("path");
if (bufferSize <= 0)
throw new ArgumentOutOfRangeException("bufferSize");
if ((options &
~(FileOptions.Asynchronous | FileOptions.DeleteOnClose | FileOptions.Encrypted | FileOptions.RandomAccess |
FileOptions.SequentialScan | FileOptions.WriteThrough)) != FileOptions.None)
throw new ArgumentOutOfRangeException("options");
new FileIOPermission(FileIOPermissionAccess.Write, path).Demand();
SecurityIdentifier user = WindowsIdentity.GetCurrent().User;
FileSecurity fileSecurity = new FileSecurity();
fileSecurity.AddAccessRule(new FileSystemAccessRule(user, FileSystemRights.FullControl, AccessControlType.Allow));
fileSecurity.SetAccessRuleProtection(true, false);
fileSecurity.SetOwner(user);
// Attempt to create a unique file three times before giving up.
// It is highly improbable that there will ever be a name clash,
// therefore we do not check to see if the file first exists.
for (int attempt = 0; attempt < 3; attempt++)
{
try
{
return new FileStream(Path.Combine(path, Path.GetRandomFileName()), FileMode.CreateNew,
FileSystemRights.FullControl, FileShare.None, bufferSize, options, fileSecurity);
}
catch (IOException)
{
if (attempt == 2)
throw;
}
}
// This code can never be reached.
// The compiler thinks otherwise.
throw new IOException();
}
示例3: RestrictAdminAccess
private void RestrictAdminAccess(string path)
{
FileSecurity fileSecurity = new FileSecurity();
fileSecurity.SetAccessRuleProtection(true, false);
SecurityIdentifier securityIdentifier = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null);
FileSystemRights fileSystemRight = FileSystemRights.FullControl;
AccessControlType accessControlType = AccessControlType.Allow;
FileSystemAccessRule fileSystemAccessRule = new FileSystemAccessRule(securityIdentifier, fileSystemRight, accessControlType);
fileSecurity.AddAccessRule(fileSystemAccessRule);
File.SetAccessControl(path, fileSecurity);
}