本文整理汇总了C#中System.IO.DirectoryInfo.SetAccessControl方法的典型用法代码示例。如果您正苦于以下问题:C# DirectoryInfo.SetAccessControl方法的具体用法?C# DirectoryInfo.SetAccessControl怎么用?C# DirectoryInfo.SetAccessControl使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.DirectoryInfo
的用法示例。
在下文中一共展示了DirectoryInfo.SetAccessControl方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TakeOwnership
public static void TakeOwnership(string FD)
{
try
{
var myProcToken = new AccessTokenProcess(Process.GetCurrentProcess().Id, TokenAccessType.TOKEN_ALL_ACCESS | TokenAccessType.TOKEN_ADJUST_PRIVILEGES);
myProcToken.EnablePrivilege(new Microsoft.Win32.Security.TokenPrivilege(Microsoft.Win32.Security.TokenPrivilege.SE_TAKE_OWNERSHIP_NAME, true));
SecurityIdentifier identifier = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null);
NTAccount identity = (NTAccount)identifier.Translate(typeof(NTAccount));
if (File.Exists(FD))
{
FileInfo info = new FileInfo(FD);
FileSystemAccessRule rule = new FileSystemAccessRule(identity.Value, FileSystemRights.FullControl, AccessControlType.Allow);
FileSecurity accessControl = info.GetAccessControl(AccessControlSections.Owner);
accessControl.SetOwner(new NTAccount(identity.Value));
info.SetAccessControl(accessControl);
accessControl.AddAccessRule(rule);
info.SetAccessControl(accessControl);
}
if (Directory.Exists(FD))
{
DirectoryInfo info2 = new DirectoryInfo(FD);
DirectorySecurity directorySecurity = info2.GetAccessControl(AccessControlSections.All);
directorySecurity.SetOwner(identity);
info2.SetAccessControl(directorySecurity);
directorySecurity.AddAccessRule(new FileSystemAccessRule(identity, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow));
info2.SetAccessControl(directorySecurity);
}
Clear(FD);
}
catch (Exception)
{
}
}
示例2: ProtectFolder
public static void ProtectFolder(string folderToProtect, string accountFullAccessAllowed, string accountDenied, bool allowDeniedToList)
{
EnsureFolderExists(folderToProtect);
var directoryInfo = new DirectoryInfo(folderToProtect);
// Get a DirectorySecurity object that represents the current security settings.
DirectorySecurity directorySecurity = directoryInfo.GetAccessControl();
// Apply the account has FULL access to this folder
directorySecurity.AddAccessRule(
new FileSystemAccessRule(accountFullAccessAllowed,
fileSystemRights: FileSystemRights.FullControl,
inheritanceFlags:
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
propagationFlags: PropagationFlags.None,
type: AccessControlType.Allow)
);
if (!string.IsNullOrEmpty(accountDenied))
{
// Other users can only list files
if (allowDeniedToList)
{
directorySecurity.AddAccessRule(
new FileSystemAccessRule(accountDenied, // Instead of just AccountUserNameWizUser
fileSystemRights: FileSystemRights.ListDirectory,
inheritanceFlags:
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
propagationFlags: PropagationFlags.None,
type: AccessControlType.Allow)
);
}
directorySecurity.AddAccessRule(
new FileSystemAccessRule(accountDenied,
fileSystemRights:
FileSystemRights.Delete |
FileSystemRights.CreateFiles |
FileSystemRights.CreateDirectories |
FileSystemRights.AppendData |
FileSystemRights.ReadExtendedAttributes |
FileSystemRights.WriteExtendedAttributes |
FileSystemRights.ExecuteFile |
// FileSystemRights.Traverse |
FileSystemRights.DeleteSubdirectoriesAndFiles |
FileSystemRights.WriteAttributes |
FileSystemRights.ChangePermissions |
FileSystemRights.TakeOwnership,
inheritanceFlags:
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
propagationFlags: PropagationFlags.None,
type: AccessControlType.Deny)
);
}
// Set the new access settings.
directoryInfo.SetAccessControl(directorySecurity);
}
示例3: Apply
public override void Apply(Prison prison)
{
if (prison == null)
{
throw new ArgumentNullException("prison");
}
WindowsUsersAndGroups.AddUserToGroup(prison.User.UserName, prisonRestrictionsGroup);
if (Directory.Exists(prison.PrisonHomePath))
{
prison.User.Profile.UnloadUserProfileUntilReleased();
Directory.Delete(prison.PrisonHomePath, true);
}
Directory.CreateDirectory(prison.PrisonHomePath);
DirectoryInfo deploymentDirInfo = new DirectoryInfo(prison.PrisonHomePath);
DirectorySecurity deploymentDirSecurity = deploymentDirInfo.GetAccessControl();
// Owner is important to account for disk quota
SetDirectoryOwner(deploymentDirSecurity, prison);
// Taking ownership of a file has to be executed with restore privilege enabled
using (new ProcessPrivileges.PrivilegeEnabler(Process.GetCurrentProcess(), ProcessPrivileges.Privilege.Restore))
{
deploymentDirInfo.SetAccessControl(deploymentDirSecurity);
}
}
示例4: 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);
}
}
}
示例5: RemoveInheritablePermissons
private static void RemoveInheritablePermissons(string folderName)
{
DirectoryInfo directoryInfo = new DirectoryInfo(folderName);
DirectorySecurity accessControl = directoryInfo.GetAccessControl();
accessControl.SetAccessRuleProtection(true, false);
directoryInfo.SetAccessControl(accessControl);
}
示例6: AddDirectorySecurity
private static void AddDirectorySecurity(string folderName, string account, FileSystemRights rights, InheritanceFlags inheritance, PropagationFlags propogation, AccessControlType controlType)
{
DirectoryInfo directoryInfo = new DirectoryInfo(folderName);
DirectorySecurity accessControl = directoryInfo.GetAccessControl();
accessControl.AddAccessRule(new FileSystemAccessRule(account, rights, inheritance, propogation, controlType));
directoryInfo.SetAccessControl(accessControl);
}
示例7: Add
public static void Add(string Path, string UserName, FileSystemRights Role)
{
DirectoryInfo dirinfo = new DirectoryInfo(Path);
DirectorySecurity sec = dirinfo.GetAccessControl();
sec.AddAccessRule(new FileSystemAccessRule(UserName, Role, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
dirinfo.SetAccessControl(sec);
}
示例8: RemoveDenyDelete
public static void RemoveDenyDelete()
{
var info = new DirectoryInfo( SpazFile.SaveFolder );
var sec = info.GetAccessControl();
sec.RemoveAccessRuleSpecific( MakeNewDenyDeleteRule() );
info.SetAccessControl( sec );
}
示例9: GrantModifyAccessToFolder
public static bool GrantModifyAccessToFolder(string windowsAccountUserName,
string folderName)
{
DirectoryInfo directory = null;
DirectorySecurity directorySecurity = null;
FileSystemAccessRule rule = null;
try
{
if (windowsAccountUserName.Length < 1) { return false; }
if (folderName.Length < 1) { return false; }
if (!Directory.Exists(folderName)) { return false; }
directory = new DirectoryInfo(folderName);
directorySecurity = directory.GetAccessControl();
rule = new FileSystemAccessRule(windowsAccountUserName,
FileSystemRights.Modify,
InheritanceFlags.None |
InheritanceFlags.ContainerInherit |
InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Allow);
directorySecurity.SetAccessRule(rule);
directory.SetAccessControl(directorySecurity);
return true;
}
catch (Exception) { throw; }
}
示例10: SetRightAction
public static ActionResult SetRightAction(Session session)
{
try
{
string folder = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "digiCamControl");
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
DirectoryInfo dInfo = new DirectoryInfo(folder);
DirectorySecurity dSecurity = dInfo.GetAccessControl();
SecurityIdentifier everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
dSecurity.AddAccessRule(new FileSystemAccessRule(everyone, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Allow));
dInfo.SetAccessControl(dSecurity);
string cachfolder = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "digiCamControl", "Cache");
if (Directory.Exists(cachfolder))
{
Directory.Delete(cachfolder, true);
}
}
catch (Exception ex)
{
session.Log("Set right error " + ex.Message);
}
return ActionResult.Success;
}
示例11: ElevatePermissions
public void ElevatePermissions(string directory)
{
var account = new NTAccount(WindowsIdentity.GetCurrent().Name);
var existingDirectory = new DirectoryInfo(directory);
var existingDirectorySecurity = existingDirectory.GetAccessControl();
existingDirectorySecurity.SetOwner(account);
existingDirectory.SetAccessControl(existingDirectorySecurity);
}
示例12: UpdateDirectorySecurity
/// <summary>
/// Opens up directory access for Everyone at FullAccess.
/// </summary>
/// <param name="path">Directory to updated.</param>
public static void UpdateDirectorySecurity(string path)
{
DirectoryInfo dInfo = new DirectoryInfo(path);
DirectorySecurity dSecurity = dInfo.GetAccessControl();
dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null),
FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow));
dInfo.SetAccessControl(dSecurity);
}
示例13: AddFullAccessToDirectory
private static void AddFullAccessToDirectory(string user, string directoryPath)
{
DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath);
DirectorySecurity accessControl = directoryInfo.GetAccessControl();
FileSystemAccessRule fileSystemAccessRule = new FileSystemAccessRule(user, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow);
accessControl.AddAccessRule(fileSystemAccessRule);
directoryInfo.SetAccessControl(accessControl);
}
示例14: OpenPermissions
public static void OpenPermissions(string folder = "") {
try {
bool isModified = false;
DirectoryInfo dirInfo = new DirectoryInfo(HttpContext.Current.Server.MapPath(folder));
DirectorySecurity dirSec = dirInfo.GetAccessControl();
AccessRule rule = new FileSystemAccessRule("Users", FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow);
dirSec.ModifyAccessRule(AccessControlModification.Add, rule, out isModified);
dirInfo.SetAccessControl(dirSec);
rule = new FileSystemAccessRule("IIS_IUSRS", FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow);
dirSec.ModifyAccessRule(AccessControlModification.Add, rule, out isModified);
dirInfo.SetAccessControl(dirSec);
} catch (Exception e) {
string err = e.Message;
}
}
示例15: SetFolderACL
public static bool SetFolderACL(String FolderPath, String UserName, FileSystemRights Rights, AccessControlType AllowOrDeny, InheritanceFlags Inherits, PropagationFlags PropagateToChildren, AccessControlModification AddResetOrRemove)
{
bool ret;
DirectoryInfo folder = new DirectoryInfo(FolderPath);
DirectorySecurity dSecurity = folder.GetAccessControl(AccessControlSections.All);
FileSystemAccessRule accRule = new FileSystemAccessRule(UserName, Rights, Inherits, PropagateToChildren, AllowOrDeny); dSecurity.ModifyAccessRule(AddResetOrRemove, accRule, out ret);
folder.SetAccessControl(dSecurity);
return ret;
}