首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> C# >> 關於C#

C# 修改檔案或資料夾權限:為指定使用者與群組新增完全控制權限

     編輯:關於C#

程式碼片段

// 給 Excel 檔案新增 "Everyone,Users" 群組完全控制權限
FileInfo fi = new FileInfo(excelPath);
FileSecurity fileSecurity = fi.GetAccessControl();
fileSecurity.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow));
fileSecurity.AddAccessRule(new FileSystemAccessRule("Users", FileSystemRights.FullControl, AccessControlType.Allow));
fi.SetAccessControl(fileSecurity);

// 給 Excel 檔案所在目錄新增 "Everyone,Users" 群組完全控制權限
DirectoryInfo di = new DirectoryInfo(Path.GetDirectoryName(excelPath));
DirectorySecurity dirSecurity = di.GetAccessControl();
dirSecurity.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow));
dirSecurity.AddAccessRule(new FileSystemAccessRule("Users", FileSystemRights.FullControl, AccessControlType.Allow));
di.SetAccessControl(dirSecurity);

使用 FileInfo/DirectoryInfo 取得 FileSecurity/DirectorySecurity,新增 FileSystemAccessRule 規則並套用。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved