C # folder permissions Action Tool class

Source: Internet
Author: User

Tag:rect   try    need    cat   std    sse   ret   obj   section   

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>//Directory permission helper///&L T;/summary> public class Dictionaryhelper {//<summary>/////-Requires sufficient permissions to perform this operation: Get filepath rig HTS//</summary>//<param name= "path" ></param>//<returns></returns&        Gt public static list<filesystemrights> Getrights (string path) {list<filesystemrights> ret = n            EW 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 = "T        Estdirectory ";        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.Allo        W);        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, right            S, 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 ob            Ject.            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, Ri            Ghts, ControlType));            Set the new access settings.        Dinfo.setaccesscontrol (dsecurity); }    }}

C # folder permissions Action Tool class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.