C#修改檔案或檔案夾的許可權,為指定使用者、使用者組添加完全控制許可權的具體代碼介紹
//給Excel檔案添加"Everyone,Users"使用者組的完全控制許可權 FileInfo fi = new FileInfo(excelPath); System.Security.AccessControl.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)); System.Security.AccessControl.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);