Setting ntfs permissions with c #

xiaoxiao2021-03-06  101

Using microsoft.win32.security;

Here's a Method to Add A Dir, And Set NTFS Permissions on It for a given user:

Private Boolean Createdir (String strusepath) {

Boolean Bok;

Try {

Directory.createdIRectory (strsitepath);

SecurityDescriptor secondsc = securityDescriptor.getFileSecurity (strsitepath, security_information.dacl_security_information);

DACL DACL = SECDESC.DACL;

SID SIDUSER = New SID (STRUSERNAME);

// Allow: Folder, Subfolder and Files

// MODIFY

dacl.AddAce (new AceAccessAllowed (sidUser, AccessType.GENERIC_WRITE | AccessType.GENERIC_READ | AccessType.DELETE | AccessType.GENERIC_EXECUTE, AceFlags.OBJECT_INHERIT_ACE | AceFlags.CONTAINER_INHERIT_ACE));

// deny: this folder

// Write Attribs

// Write Extended Attribs

// delete

// Change Permissions

// Take Ownership

DirectoryAccessType DAType = DirectoryAccessType.FILE_WRITE_ATTRIBUTES | DirectoryAccessType.FILE_WRITE_EA | DirectoryAccessType.DELETE | DirectoryAccessType.WRITE_OWNER | DirectoryAccessType.WRITE_DAC;

Accesstype atype = (accesstype) Datype;

Dacl.addace (New AceaccessDenied (SiDuser, Atype);

SECDESC.SETDACL (DACL);

SECDESC.SETFILESECURITY (STRSITEPATH, Security_INFORMATION.DACL_SECURITY_INFORMATION);

Bok = true;

} catCH {

Bok = false;

}

Return Bok;

} / * CreateDir * /

转载请注明原文地址:https://www.9cbs.com/read-97494.html

New Post(0)