public static void SetPermission(List <string> sharedList, string filePath) { try { FileInfo info = new FileInfo(filePath); FileSecurity fs = info.GetAccessControl(); /* Start of Take Ownership by Cato Yeung 2016/04/10 */ //SecurityIdentifier cu = WindowsIdentity.GetCurrent().User; //fs.SetOwner(cu); //File.SetAccessControl(filePath, fs); /* End of Take Ownership by Cato Yeung 2016/04/10 */ fs.SetAccessRuleProtection(true, false); fs.AddAccessRule(new FileSystemAccessRule(@"kmhk\itadmin", FileSystemRights.FullControl, AccessControlType.Allow)); fs.AddAccessRule(new FileSystemAccessRule(AdUtil.GetUserIdByUsername(GlobalService.User, "kmhk.local"), FileSystemRights.FullControl, AccessControlType.Allow)); foreach (string shared in sharedList) { string staffId = AdUtil.GetUserIdByUsername(shared.Trim(), "kmhk.local"); //Debug.WriteLine(shared + " " + filePath); //fs.SetAccessRuleProtection(true, false); fs.AddAccessRule(new FileSystemAccessRule(staffId, FileSystemRights.Modify, AccessControlType.Allow)); if (UserUtil.IsSpecialUser(shared)) //if (shared == "Chow Chi To(周志滔,Sammy)" || shared == "Ling Wai Man(凌慧敏,Velma)" || shared == "Chan Fai Lung(陳輝龍,Onyx)" || shared == "Ng Lau Yu, Lilith (吳柳如)" || // shared == "Lee Miu Wah(李苗華)" || shared == "Lee Ming Fung(李銘峯)" || shared == "Ho Kin Hang(何健恒,Ken)" || shared == "Yeung Wai, Gabriel (楊偉)") { string asText = string.Format("select as_userid from TB_USER_AS where as_user = N'{0}'", shared.Trim()); string asId = DataService.GetInstance().ExecuteScalar(asText).ToString().Trim(); fs.AddAccessRule(new FileSystemAccessRule(asId, FileSystemRights.Modify, AccessControlType.Allow)); } } File.SetAccessControl(filePath, fs); } catch (Exception ex) { Debug.WriteLine(ex.Message + ex.StackTrace); MessageBox.Show("Errors found when setting permission."); } }