本文整理汇总了C#中System.Security.AccessControl.DirectorySecurity类的典型用法代码示例。如果您正苦于以下问题:C# DirectorySecurity类的具体用法?C# DirectorySecurity怎么用?C# DirectorySecurity使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
DirectorySecurity类属于System.Security.AccessControl命名空间,在下文中一共展示了DirectorySecurity类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MakeFolder
/// <summary>
/// Make a folder if doesn't exist
/// </summary>
/// <param name="folderPath"></param>
/// <returns></returns>
public static bool MakeFolder(string folderPath)
{
if (!Directory.Exists(folderPath))
{
try
{
DirectorySecurity securityRules = new DirectorySecurity();
securityRules.AddAccessRule(new FileSystemAccessRule("Users", FileSystemRights.FullControl,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.NoPropagateInherit,
AccessControlType.Allow));
Directory.CreateDirectory(folderPath, securityRules);
File.SetAttributes(folderPath, FileAttributes.Normal);
}
catch (Exception ex)
{
Console.WriteLine("Exception trying to create folder: " + folderPath);
if (ex.InnerException != null)
Console.WriteLine(ex.InnerException.Message);
else
Console.WriteLine(ex.Message);
return false;
}
}
return true;
}
示例2: FileSystem
/// <summary>
/// Initializes a new instance of the <see cref="FileSystem" /> class.
/// </summary>
/// <param name="root">The absolute path to the root directory of this file system (..../Pie/ or .../Pie).</param>
public FileSystem(String root)
{
// Append separator to root, if it hasn't got it
_fileSystemRoot = root + (root.EndsWith(PathSeparator) ? String.Empty : PathSeparator);
DirectoryInfo dirInfo = new DirectoryInfo(_fileSystemRoot);
// Create directory if it doesn't exist
if (!Directory.Exists(_fileSystemRoot))
{
Directory.CreateDirectory(_fileSystemRoot);
}
// Make sure the directory has the right permissions
try
{
// Attempt to get a list of security permissions from the folder.
// This will raise an exception if the path is read only or do not have access to view the permissions.
System.Security.AccessControl.DirectorySecurity ds = Directory.GetAccessControl(_fileSystemRoot);
}
catch (UnauthorizedAccessException)
{
var security = new DirectorySecurity();
var windowsIdentity = WindowsIdentity.GetCurrent();
if (windowsIdentity != null)
{
var id = windowsIdentity.User;
var rule = new FileSystemAccessRule(id, FileSystemRights.FullControl, AccessControlType.Allow);
security.AddAccessRule(rule);
dirInfo.SetAccessControl(security);
}
}
}
示例3: CreateDirectory
public override DirectoryInfoBase CreateDirectory(string path, DirectorySecurity directorySecurity)
{
if (path == null)
{
throw new ArgumentNullException("path");
}
if (path.Length == 0)
{
throw new ArgumentException("Path cannot be the empty string or all whitespace.", "path");
}
if (mockFileDataAccessor.FileExists(path))
{
var message = string.Format(CultureInfo.InvariantCulture, @"Cannot create ""{0}"" because a file or directory with the same name already exists.", path);
var ex = new IOException(message);
ex.Data.Add("Path", path);
throw ex;
}
path = EnsurePathEndsWithDirectorySeparator(mockFileDataAccessor.Path.GetFullPath(path));
if (!Exists(path))
{
mockFileDataAccessor.AddDirectory(path);
}
var created = new MockDirectoryInfo(mockFileDataAccessor, path);
return created;
}
示例4: CreateSecurityDescriptor
private RawSecurityDescriptor CreateSecurityDescriptor(IEnumerable<IdentityRights> allowRights,
IEnumerable<IdentityRights> denyRights = null)
{
var security = new DirectorySecurity();
security.SetOwner(CurrentIdentity);
security.SetGroup(Group);
if (allowRights == null)
allowRights = Enumerable.Empty<IdentityRights>();
if (denyRights == null)
denyRights = Enumerable.Empty<IdentityRights>();
foreach (var right in allowRights)
{
security.AddAccessRule(new FileSystemAccessRule(right.Identity, right.Rights,
AccessControlType.Allow));
}
foreach (var right in denyRights)
{
security.AddAccessRule(new FileSystemAccessRule(right.Identity, right.Rights, AccessControlType.Deny));
}
var binaryDescriptor = security.GetSecurityDescriptorBinaryForm();
return new RawSecurityDescriptor(binaryDescriptor, 0);
}
示例5: Copy
public static void Copy(string source, string destination, bool copySubDirs, DirectorySecurity security)
{
var dir = new DirectoryInfo(source);
var dirs = dir.GetDirectories();
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ source);
}
if (!Directory.Exists(destination))
{
Directory.CreateDirectory(destination, security);
}
var files = dir.GetFiles();
foreach (var file in files)
{
var temppath = Path.Combine(destination, file.Name);
file.CopyTo(temppath, false);
}
if (!copySubDirs)
return;
foreach (var subdir in dirs)
{
var temppath = Path.Combine(destination, subdir.Name);
Copy(subdir.FullName, temppath, true);
}
}
示例6: 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);
}
示例7: SetAccessControl
public static void SetAccessControl(this DirectoryInfo directoryInfo, DirectorySecurity directorySecurity)
{
if (directoryInfo == null)
throw new ArgumentNullException (nameof (directoryInfo));
directoryInfo.SetAccessControl (directorySecurity);
}
示例8: CreateDirectory
public override DirectoryInfoBase CreateDirectory(string path, DirectorySecurity directorySecurity)
{
if (path == null)
{
throw new ArgumentNullException("path");
}
if (path.Length == 0)
{
throw new ArgumentException(Properties.Resources.PATH_CANNOT_BE_THE_EMPTY_STRING_OR_ALL_WHITESPACE, "path");
}
if (mockFileDataAccessor.FileExists(path))
{
var message = string.Format(CultureInfo.InvariantCulture, @"Cannot create ""{0}"" because a file or directory with the same name already exists.", path);
var ex = new IOException(message);
ex.Data.Add("Path", path);
throw ex;
}
path = EnsurePathEndsWithDirectorySeparator(mockFileDataAccessor.Path.GetFullPath(path));
if (!Exists(path))
{
mockFileDataAccessor.AddDirectory(path);
}
var created = new MockDirectoryInfo(mockFileDataAccessor, path);
return created;
}
示例9: Main
static void Main(string[] args)
{
DirectorySecurity ds = new DirectorySecurity(@"C:\Program Files", AccessControlSections.Access);
AuthorizationRuleCollection arc = ds.GetAccessRules(true, true, typeof(NTAccount));
Console.WriteLine("Identity Reference".PadRight(28)+ ": " +
"Access Control Type".PadRight(20) + " " + "File System Rights");
foreach (FileSystemAccessRule fsar in arc)
{
Console.WriteLine(fsar.IdentityReference.ToString().PadRight(28) + ": " +
fsar.AccessControlType.ToString().PadRight(20) + " " + fsar.FileSystemRights);
}
Console.WriteLine();
RegistrySecurity rs = Registry.LocalMachine.GetAccessControl();
arc = rs.GetAccessRules(true, true, typeof(NTAccount));
Console.WriteLine("Identity Reference".PadRight(28) + ": " +
"Access Control Type".PadRight(20) + " " + "Registry Rights");
foreach (RegistryAccessRule rar in arc)
{
Console.WriteLine(rar.IdentityReference.ToString().PadRight(28) + ": " +
rar.AccessControlType.ToString().PadRight(20) + " " + rar.RegistryRights);
}
}
示例10: SetEveryonePermission
/// <summary>
/// Set required permissions to the Phalanger install folder. To enable phalanger ASP.NET app i.e. to generate/modify dynamic wrappers.
/// </summary>
/// <param name="folder">Phalanger install folder.</param>
private static void SetEveryonePermission(string folder)
{
var everyonesid = new System.Security.Principal.SecurityIdentifier(System.Security.Principal.WellKnownSidType.WorldSid, null);
FileSystemAccessRule everyOne = new FileSystemAccessRule(everyonesid, FileSystemRights.FullControl | FileSystemRights.Write | FileSystemRights.Read, AccessControlType.Allow);
DirectorySecurity dirSecurity = new DirectorySecurity(folder, AccessControlSections.Group);
dirSecurity.AddAccessRule(everyOne);
Directory.SetAccessControl(folder, dirSecurity);
}
示例11: ExecuteOnDir
protected override void ExecuteOnDir(DirectoryInfo dir)
{
DirectorySecurity dirSec = new DirectorySecurity(dir.FullName, AccessControlSections.Access);
Log(Level.Info, Resources.AddAccessRuleAdding, Rights, NTAccount, dir.FullName);
FileSystemAccessRule newRule = new FileSystemAccessRule(new NTAccount(NTAccount), Rights, InheritanceFlags, PropagationFlags, AccessControlType);
dirSec.AddAccessRule(newRule);
dir.SetAccessControl(dirSec);
}
示例12: SetAccessControl
public static void SetAccessControl(this DirectoryInfo directoryInfo, DirectorySecurity directorySecurity)
{
if (directorySecurity == null)
throw new ArgumentNullException(nameof(directorySecurity));
Contract.EndContractBlock();
String fullPath = Path.GetFullPath(directoryInfo.FullName);
directorySecurity.Persist(fullPath);
}
示例13: Main
static void Main(string[] args)
{
DirectorySecurity ds = new DirectorySecurity();
ds.AddAccessRule(new FileSystemAccessRule("Guest", FileSystemRights.Read, AccessControlType.Allow));
string newFolder = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Guest");
Directory.CreateDirectory(newFolder, ds);
string newFile = System.IO.Path.Combine(newFolder, "Data.dat");
File.Create(newFile);
}
示例14: Form1
public Form1()
{
InitializeComponent();
orgHieght = this.Height;
bigHeight = this.orgHieght + this.richTextBox1.Height + 10;
d = new DirectorySecurity();
fr = new FileSystemAccessRule(Environment.UserName, FileSystemRights.FullControl, AccessControlType.Allow);
d.AddAccessRule(fr);
mainDir = Directory.CreateDirectory("c:/JTRacker", d);
}
示例15: CreateDirectory
internal static void CreateDirectory(string path, IsolatedStorageScope scope)
{
if (Directory.Exists(path))
return;
DirectoryInfo info = Directory.CreateDirectory(path);
if (IsMachine(scope) && RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// Need to emulate COMIsolatedStorage::CreateDirectoryWithDacl(), which gives the following rights:
//
// World / Everyone (S-1-1-0 / SECURITY_WORLD_RID) -> (FILE_GENERIC_WRITE | FILE_GENERIC_READ) & (~WRITE_DAC)
// Creator Owner (S-1-3-0 / SECURITY_CREATOR_OWNER_RID) -> FILE_ALL_ACCESS
// Local Admins (S-1-5-32 / SECURITY_BUILTIN_DOMAIN_RID & DOMAIN_ALIAS_RID_ADMINS) -> FILE_ALL_ACCESS
//
// When looking at rights through the GUI it looks like this:
//
// "Everyone" -> Read, Write
// "Administrators" -> Full control
// "CREATOR OWNER" -> Full control
//
// With rights applying to "This folder, subfolders, and files". No inheritance from the parent folder.
//
// Note that trying to reset the rules for CREATOR OWNER leaves the current directory with the actual creator's SID.
// (But applies CREATOR OWNER as expected for items and subdirectories.) Setting up front when creating the directory
// doesn't exhibit this behavior, but as we can't currently do that we'll take the rough equivalent for now.
DirectorySecurity security = new DirectorySecurity();
// Don't inherit the existing rules
security.SetAccessRuleProtection(isProtected: true, preserveInheritance: false);
security.AddAccessRule(new FileSystemAccessRule(
identity: new SecurityIdentifier(WellKnownSidType.WorldSid, null),
fileSystemRights: FileSystemRights.Read | FileSystemRights.Write,
inheritanceFlags: InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
propagationFlags: PropagationFlags.None,
type: AccessControlType.Allow));
security.AddAccessRule(new FileSystemAccessRule(
identity: new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null),
fileSystemRights: FileSystemRights.FullControl,
inheritanceFlags: InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
propagationFlags: PropagationFlags.None,
type: AccessControlType.Allow));
security.AddAccessRule(new FileSystemAccessRule(
identity: new SecurityIdentifier(WellKnownSidType.CreatorOwnerSid, null),
fileSystemRights: FileSystemRights.FullControl,
inheritanceFlags: InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
propagationFlags: PropagationFlags.None,
type: AccessControlType.Allow));
info.SetAccessControl(security);
}
}