本文整理汇总了C#中System.Security.AccessControl.FileSecurity类的典型用法代码示例。如果您正苦于以下问题:C# FileSecurity类的具体用法?C# FileSecurity怎么用?C# FileSecurity使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
FileSecurity类属于System.Security.AccessControl命名空间,在下文中一共展示了FileSecurity类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetOwnershipTest2
public void GetOwnershipTest2()
{
// Arrange
var tmpDir = Path.Combine(Path.GetTempPath(), "dirtools-test-" + Guid.NewGuid().ToString());
Directory.CreateDirectory(tmpDir);
var tmpFile = Path.Combine(tmpDir, "asdf");
var localSystem = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null);
var fileSec = new FileSecurity();
fileSec.SetOwner(localSystem);
File.Create(tmpFile, 1, FileOptions.None, fileSec).Dispose();
// Act
var curIdentity = new NTAccount(Environment.UserDomainName, Environment.UserName);
DirectoryTools.GetOwnershipForDirectory(tmpFile, curIdentity);
// Assert
var curFilesec = new FileSecurity(tmpFile, AccessControlSections.Owner);
IdentityReference owner = curFilesec.GetOwner(typeof(NTAccount));
Assert.IsTrue(curIdentity == owner);
}
示例2: SetAccessControl
public static void SetAccessControl(this FileInfo fileInfo, FileSecurity fileSecurity)
{
if (fileInfo == null)
throw new ArgumentNullException (nameof (fileInfo));
fileInfo.SetAccessControl (fileSecurity);
}
示例3: CreateFileOperation
public CreateFileOperation( string path, int bufferSize, FileOptions options, FileSecurity fileSecurity )
{
this.path = path;
this.bufferSize = bufferSize;
this.options = options;
this.fileSecurity = fileSecurity;
tempFilePath = Path.Combine( Path.GetTempPath(), Path.GetRandomFileName() );
}
示例4: ExecuteOnFile
protected override void ExecuteOnFile(FileInfo file)
{
FileSecurity fileSec = new FileSecurity(file.FullName, AccessControlSections.Access);
Log(Level.Info, Resources.AddAccessRuleAdding, Rights, NTAccount, file.FullName);
FileSystemAccessRule newRule = new FileSystemAccessRule(new NTAccount(NTAccount), Rights, AccessControlType);
fileSec.AddAccessRule(newRule);
file.SetAccessControl(fileSec);
}
示例5: SetAccessControl
public static void SetAccessControl(this FileInfo fileInfo, FileSecurity fileSecurity)
{
if (fileSecurity == null)
throw new ArgumentNullException(nameof(fileSecurity));
Contract.EndContractBlock();
String fullPath = Path.GetFullPath(fileInfo.FullName);
// Appropriate security check should be done for us by FileSecurity.
fileSecurity.Persist(fullPath);
}
示例6: SecureFile
public SecureFile(string filePath)
{
Condition.Requires(filePath).IsNotNullOrEmpty();
this.FilePath = filePath;
FileSecurity fs = new FileSecurity();
// Create a file using the FileStream class.
this.Stream = FileUtil.GetLockedStream(this.FilePath);
this.Stream.Lock(0, this.Stream.Length);
}
示例7: getFileOwner
private string getFileOwner(string filename)
{
FileSecurity tmp = new FileSecurity(filename, AccessControlSections.Owner);
string owner = "<unknown>";
try
{
owner = tmp.GetOwner(System.Type.GetType("System.Security.Principal.SecurityIdentifier")).Value;
}
catch { }
return owner;
}
示例8: Main
static void Main(string[] args)
{
DirectorySecurity ds = new DirectorySecurity();
ds.AddAccessRule(new FileSystemAccessRule("Rafa&Pri", FileSystemRights.Read, AccessControlType.Allow));
string newFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Rafa&Pri");
Directory.CreateDirectory(newFolder, ds);
FileSecurity fs = new FileSecurity();
fs.AddAccessRule(new FileSystemAccessRule("Rafa&Pri", FileSystemRights.FullControl, AccessControlType.Allow));
string newFile = Path.Combine(newFolder, "Data.dat");
File.Create(newFile, 100, FileOptions.None, fs);
}
示例9: AllowFileAccess
public static bool AllowFileAccess(string path, FileSystemRights rights)
{
try
{
var security = new System.Security.AccessControl.FileSecurity();
var usersSid = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
security.AddAccessRule(new FileSystemAccessRule(usersSid, rights, AccessControlType.Allow));
File.SetAccessControl(path, security);
return true;
}
catch
{
return false;
}
}
示例10: ExecuteOnFile
protected override void ExecuteOnFile(FileInfo file)
{
FileSecurity fileSec = new FileSecurity(file.FullName, AccessControlSections.Access);
IList<FileSystemAccessRule> targetRules = FindAccessRules(fileSec);
if (targetRules.Count == 0)
{
Log(Level.Info, Resources.RemoveAccessRuleEmpty, NTAccount, file.FullName);
}
else
{
foreach (FileSystemAccessRule fileSystemAccessRule in targetRules)
{
Log(Level.Info, Resources.RemoveAccessRuleRemoving, NTAccount, file.FullName);
fileSec.RemoveAccessRule(fileSystemAccessRule);
}
file.SetAccessControl(fileSec);
}
}
示例11: CreateFile
public IFile CreateFile(string file, FileMode fileMode, FileAccess fileAccess, FileShare fileShare,
FileSystemRights fileSystemRights, FileOptions fileOptions, FileSecurity fileSecurity)
{
using (var tranHandle = GetKtmTransactionHandle())
{
int dwFlagsAndAttributes = (int) fileOptions;
dwFlagsAndAttributes |= 0x100000;
NativeMethods.FileAccess faccess = NativeFileEnums.TranslateFileAccess(fileAccess);
NativeMethods.FileShare fshare = NativeFileEnums.TranslateFileShare(fileShare);
NativeMethods.FileMode fmode = NativeFileEnums.TranslateFileMode(fileMode);
using (SafeFileHandle fileHandle = NativeMethods.CreateFileTransacted(file, faccess, fshare, IntPtr.Zero, fmode,
dwFlagsAndAttributes, IntPtr.Zero, tranHandle,
IntPtr.Zero, IntPtr.Zero))
{
if (fileHandle.IsInvalid)
throw new InvalidOperationException();
}
return GetFile(file);
}
}
示例12: GetSecurityDescriptor
/// <summary>
/// Gets the SecurityDescriptor at the specified path, including only the specified
/// AccessControlSections.
/// </summary>
///
/// <param name="path">
/// The path of the item to retrieve. It may be a drive or provider-qualified path and may include.
/// glob characters.
/// </param>
///
/// <param name="sections">
/// The sections of the security descriptor to include.
/// </param>
///
/// <returns>
/// Nothing. An object that represents the security descriptor for the item
/// specified by path is written to the context's pipeline.
/// </returns>
///
/// <exception cref="System.ArgumentException">
/// path is null or empty.
/// path doesn't exist
/// sections is not valid.
/// </exception>
public void GetSecurityDescriptor(string path,
AccessControlSections sections)
{
ObjectSecurity sd = null;
path = NormalizePath(path);
if (String.IsNullOrEmpty(path))
{
throw PSTraceSource.NewArgumentNullException("path");
}
if ((sections & ~AccessControlSections.All) != 0)
{
throw PSTraceSource.NewArgumentException("sections");
}
var currentPrivilegeState = new PlatformInvokes.TOKEN_PRIVILEGE();
try
{
PlatformInvokes.EnableTokenPrivilege("SeBackupPrivilege", ref currentPrivilegeState);
if (Directory.Exists(path))
{
sd = new DirectorySecurity(path, sections);
}
else
{
sd = new FileSecurity(path, sections);
}
}
catch (System.Security.SecurityException e)
{
WriteError(new ErrorRecord(e, e.GetType().FullName, ErrorCategory.PermissionDenied, path));
}
finally
{
PlatformInvokes.RestoreTokenPrivilege("SeBackupPrivilege", ref currentPrivilegeState);
}
WriteSecurityDescriptorObject(sd, path);
}
示例13: Create
public static FileStream Create (string path, int bufferSize,
FileOptions options,
FileSecurity fileSecurity)
{
return new FileStream (path, FileMode.Create, FileAccess.ReadWrite,
FileShare.None, bufferSize, options);
}
示例14: Create
public FileStream Create(string path, int bufferSize, FileOptions options, FileSecurity fileSecurity)
{
return File.Create(path, bufferSize, options, fileSecurity);
}
示例15: SetAccessControl
public void SetAccessControl(string path, FileSecurity fileSecurity)
{
File.SetAccessControl(path, fileSecurity);
}