C # settings and get directory permissions (. NET control ACL)

Source: Internet
Author: User
Tags readline static class

You want to use C # to set up and read directory permissions on NTFS partitions, and find a lot of information. Finally found a section of vb.net code, made changes to C # to show you.

using System;


using System.Collections;


using System.IO;


using System.Security.AccessControl;


Static Class Tester


{


public static void Main ()


  {


Try


    {


string filename = @ "f:\k"; Target directory


string account = @ "Administrator";//Username


string userrights = @ "RW";//permission string, defined by


adddirectorysecurity (filename, account, userrights);


Console.ReadLine ();


    }


catch (Exception e)


    {


Console.WriteLine (e);


Console.ReadLine ();


    }


  }


static public void Adddirectorysecurity (string FileName, String account, String userrights)


  {


filesystemrights Rights = new Filesystemrights ();


if (Userrights.indexof ("R") >= 0)


    {


Rights = Rights | Filesystemrights.read;


    }


if (Userrights.indexof ("C") >= 0)


    {


Rights = Rights | Filesystemrights.changepermissions;


    }


if (Userrights.indexof ("F") >= 0)


    {


Rights = Rights | Filesystemrights.fullcontrol;


    }


if (Userrights.indexof ("W") >= 0)


    {


Rights = Rights | Filesystemrights.write;


    }


bool OK;


DirectoryInfo dinfo = new DirectoryInfo (FileName);


DirectorySecurity dsecurity = Dinfo.getaccesscontrol ();


inheritanceflags iflags = new InheritanceFlags ();


iflags = Inheritanceflags.containerinherit | Inheritanceflags.objectinherit;


FileSystemAccessRule AccessRule2 = new FileSystemAccessRule (account, Rights, IFlags, Propagationflags.none, Acce Sscontroltype.allow);


Dsecurity.modifyaccessrule (Accesscontrolmodification.add, AccessRule2, out OK);


Dinfo.setaccesscontrol (dsecurity);


//Lists the permissions that the target directory has


directorysecurity sec = Directory.getaccesscontrol (FileName, Accesscontrolsections.all);


foreach (FileSystemAccessRule rule in sec. Getaccessrules (True, True, typeof (System.Security.Principal.NTAccount))


    {


Console.WriteLine ("----------------------------------");


Console.WriteLine (rule. Identityreference.value);


if (rule. Filesystemrights & Filesystemrights.read)!= 0)


Console.WriteLine (rule. Filesystemrights.tostring ());


    }


Console.read ();


  }


}

With MSDN, it's easy to read the code above. But it looks like this program needs to run as an administrator. ^_^

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.