2007年12月9日 星期日

Visual C#»目錄&子目錄權限

參考網址 :
http://forums.microsoft.com/MSDN-CHT/ShowPost.aspx?PostID=524250&SiteID=14

您可以透過以下的程式碼設定目錄的存取權限:
FileSecurity fSecurity = File.GetAccessControl("C:/Temp");
fSecurity.AddAccessRule(new FileSystemAccessRule(@"Domain\帳號", FileSystemRights.Read, AccessControlType.Allow));
File.SetAccessControl("C:/Temp", fSecurity);


要套用到子目錄和檔案, 必須使用DirectorySecurityAccessRule類別:
DirectoryInfo di = new DirectoryInfo(Path);
DirectorySecurity ds = di.GetAccessControl();

FileSystemAccessRule ar1 = new FileSystemAccessRule(Account, FileSystemRights.Read, AccessControlType.Allow);

FileSystemAccessRule ar2 = new FileSystemAccessRule(Account, FileSystemRights.Read, InheritanceFlags.ContainerInherit InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow);

ds.AddAccessRule(ar1);
ds.AddAccessRule(ar2);
di.SetAccessControl(ds);

沒有留言: