本文整理匯總了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);
}