C#设置文件(夹)权限
C#设置账户对文件的访问权限为完全访问
/// <summary> /// 将账户对文件的访问权限设置为完全访问 /// </summary> /// <param name="filePath">文件路径</param> /// <param name="userName">账户名</param> private void SetFilePermission(string filePath,string userName) { FileInfo fi = new FileInfo(filePath); FileSecurity fileSecurity = fi.GetAccessControl(); fileSecurity.AddAccessRule (new FileSystemAccessRule (userName, FileSystemRights.FullControl, AccessControlType.Allow)); fi.SetAccessControl(fileSecurity); }
C#设置账户对文件夹的访问权限为完全访问
/// <summary> /// 将账户对文件夹的访问权限设置为完全访问 /// </summary> /// <param name="folderPath">文件夹路径</param> /// <param name="userName">账户名</param> private void SetFolderPermission(string folderPath,string userName) { DirectoryInfo diInfo = new DirectoryInfo(folderPath); DirectorySecurity dirSecurity = diInfo.GetAccessControl(); dirSecurity.AddAccessRule (new FileSystemAccessRule (userName, FileSystemRights.FullControl, AccessControlType.Allow)); diInfo.SetAccessControl(dirSecurity); }