本文整理匯總了C#中System.IO.DirectoryInfo.GetAccessControl方法的典型用法代碼示例。如果您正苦於以下問題:C# DirectoryInfo.GetAccessControl方法的具體用法?C# DirectoryInfo.GetAccessControl怎麽用?C# DirectoryInfo.GetAccessControl使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.IO.DirectoryInfo
的用法示例。
在下文中一共展示了DirectoryInfo.GetAccessControl方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: 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);
}
示例2: RemoveDenyDelete
public static void RemoveDenyDelete()
{
var info = new DirectoryInfo( SpazFile.SaveFolder );
var sec = info.GetAccessControl();
sec.RemoveAccessRuleSpecific( MakeNewDenyDeleteRule() );
info.SetAccessControl( sec );
}
示例3: GetDirectoryData
private void GetDirectoryData(DirectoryInfo directoryInfo, DirectoryData parent)
{
// Get directory access control
DirectorySecurity accessControl;
try
{
accessControl = directoryInfo.GetAccessControl(AccessControlSections.Access);
}
catch (UnauthorizedAccessException ex)
{
OnErrorOccurred("Can't get access to directory " + directoryInfo.Name, ex);
throw;
}
// Get current directory properties
FileSystemObjectProperties properties = GetFileSystemObjectProperties(directoryInfo, accessControl);
var directoryData = new DirectoryData(properties, parent);
if (parent != null)
parent.AddFileSystemObject(directoryData);
OnGotNewFileSystemData(directoryData);
// Get data for all nested files and directories
foreach (DirectoryInfo directory in directoryInfo.GetDirectories())
{
if (_isAborted)
return;
GetDirectoryData(directory, directoryData);
}
foreach (FileInfo file in directoryInfo.GetFiles())
{
if (_isAborted)
return;
GetFileData(file, directoryData);
}
}
示例4: RemoveInheritablePermissons
private static void RemoveInheritablePermissons(string folderName)
{
DirectoryInfo directoryInfo = new DirectoryInfo(folderName);
DirectorySecurity accessControl = directoryInfo.GetAccessControl();
accessControl.SetAccessRuleProtection(true, false);
directoryInfo.SetAccessControl(accessControl);
}
示例5: AddAccessRuleTo
private void AddAccessRuleTo(FileSystemAccessRule accessRule, string path)
{
var pathInfo = new DirectoryInfo(path);
if (pathInfo.Exists)
{
log.Trace("Adding access rule to path '{0}'", pathInfo.FullName);
DirectorySecurity pathSecurity = pathInfo.GetAccessControl();
pathSecurity.AddAccessRule(accessRule);
ReplaceAllChildPermissions(pathInfo, pathSecurity);
}
else
{
DirectoryInfo parentInfo = pathInfo.Parent;
if (parentInfo.Exists)
{
log.Trace("Adding access rule to path '{0}' via parent '{1}'", pathInfo.FullName, parentInfo.FullName);
DirectorySecurity pathSecurity = parentInfo.GetAccessControl();
pathSecurity.AddAccessRule(accessRule);
Directory.CreateDirectory(pathInfo.FullName, pathSecurity);
ReplaceAllChildPermissions(pathInfo, pathSecurity);
}
}
}
示例6: 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);
}
示例7: 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;
}
示例8: 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)
{
}
}
示例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: 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);
}
示例11: AddPathPower
/// <summary>
/// 如果用戶沒有在權限列表中,則不能自動加入。
/// </summary>
/// <param name="pathname"></param>
/// <param name="username"></param>
/// <param name="power">FullControl | ReadOnly
/// | Write | Modify</param>
public static void AddPathPower(string pathname, string username, string power)
{
if (!System.IO.Directory.Exists(pathname))
{
System.IO.Directory.CreateDirectory(pathname);
}
DirectoryInfo dirinfo = new DirectoryInfo(pathname);
if ((dirinfo.Attributes & FileAttributes.ReadOnly) != 0)
{
dirinfo.Attributes = FileAttributes.Normal;
}
//取得訪問控製列表
DirectorySecurity dirsecurity = dirinfo.GetAccessControl();
switch (power)
{
case "FullControl":
dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Allow));
break;
case "ReadOnly":
dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.Read, AccessControlType.Allow));
break;
case "Write":
dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.Write, AccessControlType.Allow));
break;
case "Modify":
dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.Modify, AccessControlType.Allow));
break;
}
}
示例12: 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);
}
}
示例13: TestIfUserExistsInAD
private void TestIfUserExistsInAD(List<User> listOfUsers)
{
Directory.CreateDirectory(@"\\nas.unil.ch\FBM\SYS\TEST");
int x = 0;
foreach (User user in listOfUsers)
{
mw.Log("Try for User : " + user.Username, x);
try
{
DirectoryInfo info = new DirectoryInfo(@"\\nas.unil.ch\FBM\SYS\TEST");
DirectorySecurity dirSecurity = info.GetAccessControl();
dirSecurity.SetAccessRuleProtection(true, false);
dirSecurity.AddAccessRule(new FileSystemAccessRule(@"ad\" + user.Username, FileSystemRights.Write, AccessControlType.Allow));
Directory.SetAccessControl(@"\\nas.unil.ch\FBM\SYS\TEST", dirSecurity);
mw.Log(user.Username + " exists", x);
}
catch
{
mw.Log(user.Username + " doesn't exist", x);
mw.LogError(user.Departement + " : " + user.Username);
}
x++;
}
}
示例14: Checker
public void Checker()
{
while (Thread.CurrentThread.IsAlive)
{
Directory.CreateDirectory(@"\\nas.unil.ch\FBM\SYS\TEST");
int x = 0;
foreach (User user in listOfUsers)
{
util.Log("Try for User : " + user.Username, x);
try
{
DirectoryInfo info = new DirectoryInfo(@"\\nas.unil.ch\FBM\SYS\TEST");
DirectorySecurity dirSecurity = info.GetAccessControl();
dirSecurity.SetAccessRuleProtection(true, false);
dirSecurity.AddAccessRule(new FileSystemAccessRule(@"ad\" + user.Username, FileSystemRights.Write, AccessControlType.Allow));
Directory.SetAccessControl(@"\\nas.unil.ch\FBM\SYS\TEST", dirSecurity);
util.Log(user.Username + " exists", x);
}
catch
{
util.Log(user.Username + " doesn't exist", x);
util.LogError(user.Departement + " : " + user.Username);
}
x++;
}
Thread.CurrentThread.Abort();
}
}
示例15: 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);
}