[英]C# Storing Folder Permissions
我在存儲文件夾權限時遇到了一些麻煩。 我可以找到一些有關編寫和閱讀文檔的文檔。 我想做的是讀取特定用戶的文件夾上的權限>存儲它>更改權限>安裝程序完成后,將權限改回來。
除了如何存儲原始文件夾權限並將其重新設置之外,我所有其他功能(僅歸因於其他許多代碼)。 我很樂意閱讀您建議的任何材料,我們在軟件中收到一些致命錯誤,這是解決許多錯誤的第一步。 歡迎和感謝所有幫助。
以下是我如何設置權限的示例。 是的,我知道我有每個人,但這只是用於測試
public void setPermDir()
{
try
{
string DirectoryName = "C:\\Temp1\\";
Console.WriteLine("Adding access control entry for " + DirectoryName);
// Add the access control entry to the directory.
AddDirectorySecurity(DirectoryName, @"Everyone", FileSystemRights.FullControl, AccessControlType.Allow);
Console.WriteLine("Done.");
}
catch (Exception e)
{
Console.WriteLine(e);
}
Console.ReadLine();
}
// Adds an ACL entry on the specified directory for the specified account.
public static void AddDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
{
// Create a new DirectoryInfo object.
DirectoryInfo dInfo = new DirectoryInfo(FileName);
// Get a DirectorySecurity object that represents the
// current security settings.
DirectorySecurity dSecurity = dInfo.GetAccessControl();
// Add the FileSystemAccessRule to the security settings.
dSecurity.AddAccessRule(new FileSystemAccessRule(Account,
Rights,
ControlType));
// Set the new access settings.
dInfo.SetAccessControl(dSecurity);
}
如果從AddDirectorySecurity返回DirectorySecurity dSecurity,則可以僅調用Directory.SetAccessControl(directoryName, dSecurity);
完成修改后的訪問規則后。
更新資料
如果僅SetAccessControl不起作用,則下一步可能是顯式刪除使用FileSystemSecurity.RemoveAccessRule授予的權限。
只需保留對您創建的FileSystemAccessRule的引用:
FileSystemAccessRule toRemoveWhenDone = new FileSystemAccessRule(Account, Rights, ControlType);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.