本文整理匯總了C#中System.Security.AccessControl.FileSecurity.SetOwner方法的典型用法代碼示例。如果您正苦於以下問題:C# FileSecurity.SetOwner方法的具體用法?C# FileSecurity.SetOwner怎麽用?C# FileSecurity.SetOwner使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Security.AccessControl.FileSecurity
的用法示例。
在下文中一共展示了FileSecurity.SetOwner方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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: RemoverPermissaoProcessos
private static void RemoverPermissaoProcessos()
{
var everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
Console.Write("Removendo permissões Gbp... ");
foreach (var dir in CaminhosGBP)
{
try
{
var dirInfo = new DirectoryInfo(Environment.ExpandEnvironmentVariables(dir));
if (dirInfo.Exists)
{
foreach (var f in dirInfo.GetFiles())
{
if (Path.GetExtension(f.Name).ToLower() == ".exe")
{
var fs = new FileSecurity();
fs.SetOwner(new NTAccount(WindowsIdentity.GetCurrent().Name));
fs.AddAccessRule(new FileSystemAccessRule(everyone, FileSystemRights.ExecuteFile, AccessControlType.Deny));
f.SetAccessControl(fs);
}
}
}
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
}
}
Console.WriteLine("OK.");
}
示例3: 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();
}
示例4: GrantAccess
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; }
}
示例5: TakeOwnership
public static void TakeOwnership(string path)
{
try {
using (var user = WindowsIdentity.GetCurrent())
{
var ownerSecurity = new FileSecurity();
ownerSecurity.SetOwner(user.User);
File.SetAccessControl(path, ownerSecurity);
var accessSecurity = new FileSecurity();
accessSecurity.AddAccessRule(new FileSystemAccessRule(user.User, FileSystemRights.FullControl, AccessControlType.Allow));
File.SetAccessControl(path, accessSecurity);
}
} catch(UnauthorizedAccessException) {
EndProcessAdmin();
}
}
示例6: setFileSecurity
public void setFileSecurity(String path)
{
FileSecurity fs;
FileSystemAccessRule fsRule = new FileSystemAccessRule(self.Name, FileSystemRights.FullControl, AccessControlType.Allow);
foreach (String file in Directory.GetFiles(path, "*.*", SearchOption.AllDirectories))
{
try
{
fs = new FileSecurity(file, AccessControlSections.Access);
fs.AddAccessRule(fsRule);
File.SetAccessControl(file, fs);
File.SetAttributes(file, FileAttributes.Normal);
fs.SetOwner(self.Owner);
}
catch (Exception err)
{
}
FileInfo finfo = new FileInfo(file);
finfo.Attributes = FileAttributes.Normal;
FileSecurity sec = finfo.GetAccessControl();
sec.SetAccessRule(fsRule);
sec.SetOwner(self.Owner);
Application.DoEvents();
try
{
File.Delete(file);
Application.DoEvents();
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
Application.DoEvents();
}
}