. Net (C #): Use objectsecurity. setaccessruleprotection to retain access control data

Source: Internet
Author: User

Because Windows Access control can be inherited, when a security object is moved from one location to another, its access control may be affected. In order to allow access control of a security object to keep the existing data without inheriting the control options of the target path because of path changes. You can use the objectsecurity. setaccessruleprotection method. Note that the objectsecurity type is in the system. Security. accesscontrol namespace, And the objectsecurity and setauditruleprotection Methods correspond to the system access control list (SACL ).

 

The setaccessruleprotection method has two bool parameters. The first bool specifies whether inheritance is prohibited. The second bool specifies whether to retain the inherited access control. Of course, if the first parameter is false (inheritance is allowed), the value of the second parameter can be omitted because it will always retain the inheritance.

 

Finally, we use code to demonstrate how to copy a file to another location and retain the inheritance option of the source file and disable the inheritance option of the target directory:

// + Using system. IO;

// + Using system. Security. accesscontrol;

Static void copy (string SRC, string DEST)

{

File. Copy (SRC, DEST );

Filesecurity SECURITY = file. getaccesscontrol (SRC );

// Settings: do not allow inheritance, retain existing inherited

Security. setaccessruleprotection (True, true );

File. setaccesscontrol (DEST, security );

}

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.