C#文件夹权限操作工具类
生活随笔
收集整理的這篇文章主要介紹了
C#文件夹权限操作工具类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;namespace CvNetVideo.Play
{/// <summary>/// 目錄權限輔助類/// </summary>public class DictionaryHelper{/// <summary>/// 需要足夠的權限才能執行此操作:Get filepath rights/// </summary>/// <param name="path"></param>/// <returns></returns>public static List<FileSystemRights> GetRights(string path){List<FileSystemRights> ret = new List<FileSystemRights>();DirectorySecurity dirSec = Directory.GetAccessControl(path, AccessControlSections.All);AuthorizationRuleCollection rules = dirSec.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount));foreach (FileSystemAccessRule rule in rules){ret.Add(rule.FileSystemRights);}return ret;}//public static void AccessExample() {// try// {// string DirectoryName = "TestDirectory";// Console.WriteLine("Adding access control entry for " + DirectoryName);// // Add the access control entry to the directory.// AddDirectorySecurity(DirectoryName, @"MYDOMAIN\MyAccount", FileSystemRights.ReadData, AccessControlType.Allow);// Console.WriteLine("Removing access control entry from " + DirectoryName);// // Remove the access control entry from the directory.// RemoveDirectorySecurity(DirectoryName, @"MYDOMAIN\MyAccount", FileSystemRights.ReadData, AccessControlType.Allow);// Console.WriteLine("Done.");// }// catch (Exception e)// {// Console.WriteLine(e);// }// Console.ReadLine();//}// Adds an ACL entry on the specified directory for the specified account.public static void AddDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType){// Create a new DirectoryInfo object.DirectoryInfo dInfo = new DirectoryInfo(FileName);// Get a DirectorySecurity object that represents the // current security settings.DirectorySecurity dSecurity = dInfo.GetAccessControl();// Add the FileSystemAccessRule to the security settings. dSecurity.AddAccessRule(new FileSystemAccessRule(Account,Rights,ControlType));// Set the new access settings.dInfo.SetAccessControl(dSecurity);}// Removes an ACL entry on the specified directory for the specified account.public static void RemoveDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType){// Create a new DirectoryInfo object.DirectoryInfo dInfo = new DirectoryInfo(FileName);// Get a DirectorySecurity object that represents the // current security settings.DirectorySecurity dSecurity = dInfo.GetAccessControl();// Add the FileSystemAccessRule to the security settings. dSecurity.RemoveAccessRule(new FileSystemAccessRule(Account,Rights,ControlType));// Set the new access settings.dInfo.SetAccessControl(dSecurity);}}
}
?
轉載于:https://www.cnblogs.com/boonya/p/9013564.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的C#文件夹权限操作工具类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据结构与算法 —— 二叉树
- 下一篇: 使用nginx缓存服务器上的静态文件