Microsoft KB Archive/899553

= How to programmatically set NTFS file system folder permissions by using Active Directory Service Interfaces in Microsoft Visual C# =

Article ID: 899553

Article Last Modified on 1/15/2007

-

APPLIES TO


 * Microsoft Visual C# 2005 Express Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-





INTRODUCTION
This step-by-step article describes how to programmatically set NTFS file system folder permissions by using Active Directory Service Interfaces (ADSI) in Microsoft Visual C#.



Build the sample application
To run the following sample application, you must have the ADsSecurity.dll file and the ADsSecurity.dll file installed. These files are included with the software development kit (SDK) for Active Directory Service Interfaces 2.5. To download the SDK for Active Directory Service Interfaces 2.5, visit the following Microsoft Web site:

http://www.microsoft.com/technet/archive/winntas/downloads/adsi25.mspx?mfr=true

Note To run the sample application, you must have administrative credentials on the computer.

To build the sample application, follow these steps:  Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, click New, and then click Project. In Visual C# Projects, click Windows Application under Templates.

Note In Visual Studio 2005, Visual C# Projects is changed to Visual C#. In the Name box, type NTFSPermissions, and then click OK. Add a Button control to the Form1 form. On the Project menu, click Add Reference. Click the COM tab, click to select the following items, and then click OK: <ul> Active DS Type Library</li> ADsSecurity 2.5 Type Library</li></ul> </li> Right-click the Form1 form, and then click View Code.</li>  Add the following using statements to the top of the source code in the Form1 form. using ADSSECURITYLib; using ActiveDs; </li>  Add the following method to the Form1 class. public void SetPermissions(String vPath, String UserName ) {   ADsSecurity objADsSec; SecurityDescriptor objSecDes; AccessControlList objDAcl; AccessControlEntry objAce1; AccessControlEntry objAce2; Object objSIdHex; ADsSID objSId;

objADsSec = new ADsSecurityClass; objSecDes = (SecurityDescriptor) (objADsSec.GetSecurityDescriptor(&quot;FILE://&quot; + vPath)); objDAcl = (AccessControlList)objSecDes.DiscretionaryAcl; objSId = new ADsSIDClass; objSId.SetAs((int)ADSSECURITYLib.ADS_SID_FORMAT.ADS_SID_SAM, UserName.ToString); objSIdHex = objSId.GetAs((int)ADSSECURITYLib.ADS_SID_FORMAT.ADS_SID_SDDL);

// Add a new access control entry (ACE) object (objAce) so that the user has Full Control permissions on NTFS file system files. objAce1 = new AccessControlEntryClass; objAce1.Trustee = (objSIdHex).ToString; objAce1.AccessMask = (int)ActiveDs.ADS_RIGHTS_ENUM.ADS_RIGHT_GENERIC_ALL; objAce1.AceType = (int)ActiveDs.ADS_ACETYPE_ENUM.ADS_ACETYPE_ACCESS_ALLOWED; objAce1.AceFlags = (int)ActiveDs.ADS_ACEFLAG_ENUM.ADS_ACEFLAG_INHERIT_ACE | (int)ActiveDs.ADS_ACEFLAG_ENUM.ADS_ACEFLAG_INHERIT_ONLY_ACE | 1; objDAcl.AddAce(objAce1);

// Add a new access control entry object (objAce) so that the user has Full Control permissions on NTFS file system folders. objAce2 = new AccessControlEntryClass; objAce2.Trustee = (objSIdHex).ToString; objAce2.AccessMask = (int)ActiveDs.ADS_RIGHTS_ENUM.ADS_RIGHT_GENERIC_ALL; objAce2.AceType = (int)ActiveDs.ADS_ACETYPE_ENUM.ADS_ACETYPE_ACCESS_ALLOWED; objAce2.AceFlags = (int)ActiveDs.ADS_ACEFLAG_ENUM.ADS_ACEFLAG_INHERIT_ACE | 1; objDAcl.AddAce(objAce2);

objSecDes.DiscretionaryAcl = objDAcl;

// Set permissions on the NTFS file system folder. objADsSec.SetSecurityDescriptor(objSecDes,&quot;FILE://&quot; + vPath);

} </li> Click the Form1.cs [Design] tab to switch back to design mode.</li>  Double-click button1. Replace the button1_Click event code with the following code. private void button1_Click(object sender, System.EventArgs e) { try {       // Set <Domain> to your domain name. // Set <UserName> to the user account. SetPermissions(&quot;C:\\Test&quot;, &quot;<Domain>\\<UserName>&quot;); MessageBox.Show(&quot;Full Access control granted.&quot;); }   catch (Exception ex) {       MessageBox.Show(ex.Message); } } Note Replace   with the domain name. Replace  with the name of the user to whom you want to grant permissions. </li> On the Build menu, click Build Solution.</li></ol>

Test the sample application
<ol> Create a folder in the drive C root folder. Name the folder Test.</li> In Windows Explorer, right-click the Test folder, and then click Properties.</li> In the Test Properties dialog box, click the Security tab.</li> Select the domain account for which you are running this test. If the account is not listed, click Add, and then add the domain account to the list.</li> Under Permissions, click to clear the Full Control check box to restrict the permissions on the Test folder for this user. Then, click OK.</li> Run the NTFSPermission.exe application. By default, Form1 is displayed.</li> Click button1. You receive the following message:

Full Access control granted.

</li> <li>Click OK to close the message box.</li> <li>Close the form to quit the application.</li> <li>In Windows Explorer, open the C:\ folder.</li> <li>Right-click the Test folder, and then click Properties.</li> <li>In the Test Properties dialog box, click the Security tab.</li> <li>Select the domain account for which you are running this test, and then verify the permissions on the Test folder.</li></ol>

The specified user now has Full Control permissions on the Test folder.

<div class="references_section">