Microsoft Knowledge Base Email Alertz

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

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
KBAlertz referrals get
** SIX MONTHS FREE **


Community Site



We Send hundreds of thousands of emails using ASP.NET Email


ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 899553 - Last Review: January 15, 2007 - Revision: 2.6

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

On This Page

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#.

MORE INFORMATION

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://technet.microsoft.com/en-us/library/cc749949.aspx (http://technet.microsoft.com/en-us/library/cc749949.aspx)
Note To run the sample application, you must have administrative credentials on the computer.

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

    Note In Visual Studio 2005, Visual C# Projects is changed to Visual C#.
  4. In the Name box, type NTFSPermissions, and then click OK.
  5. Add a Button control to the Form1 form.
  6. On the Project menu, click Add Reference.
  7. Click the COM tab, click to select the following items, and then click OK:
    • Active DS Type Library
    • ADsSecurity 2.5 Type Library
  8. Right-click the Form1 form, and then click View Code.
  9. Add the following using statements to the top of the source code in the Form1 form.
    using ADSSECURITYLib;
    using ActiveDs;
  10. 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("FILE:/" + 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,"FILE:/" + vPath);
    
    }
    
  11. Click the Form1.cs [Design] tab to switch back to design mode.
  12. 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("C:\\Test", "<Domain>\\<UserName>");
    		MessageBox.Show("Full Access control granted.");
    	}
    	catch (Exception ex)
    	{
    		MessageBox.Show(ex.Message);
    	}
    }
    Note Replace <Domain> with the domain name. Replace <UserName> with the name of the user to whom you want to grant permissions.
  13. On the Build menu, click Build Solution.

Test the sample application

  1. Create a folder in the drive C root folder. Name the folder Test.
  2. In Windows Explorer, right-click the Test folder, and then click Properties.
  3. In the Test Properties dialog box, click the Security tab.
  4. 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.
  5. Under Permissions, click to clear the Full Control check box to restrict the permissions on the Test folder for this user. Then, click OK.
  6. Run the NTFSPermission.exe application. By default, Form1 is displayed.
  7. Click button1. You receive the following message:
    Full Access control granted.
  8. Click OK to close the message box.
  9. Close the form to quit the application.
  10. In Windows Explorer, open the C:\ folder.
  11. Right-click the Test folder, and then click Properties.
  12. In the Test Properties dialog box, click the Security tab.
  13. Select the domain account for which you are running this test, and then verify the permissions on the Test folder.
The specified user now has Full Control permissions on the Test folder.

REFERENCES

For more information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
279682  (http://kbalertz.com/Feedback.aspx?kbNumber=279682/ ) How to use ADsSecurity.dll to add an access control entry to an NTFS folder
266461  (http://kbalertz.com/Feedback.aspx?kbNumber=266461/ ) How to use ADSI to set automatic inheritance of file/folder permissions

APPLIES TO
  • Microsoft Visual C# 2005 Express Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
Keywords: 
kbprogramming kbpermissions kbactivedirectory kbhowto kbinfo KB899553
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Kant Report As Irrelevant  
Written: 7/4/2005 10:27 PM
Great article ! This is a better way to set ACL than using Win32 API.

Sandy D Report As Irrelevant  
Written: 8/12/2005 7:01 AM
D-Uh! How about an equally simple GetPermissions code? None of the _temp file creation_ workarounds either. All I need is to know whether I can Write to a destination folder. No SIDs, No ADSI lookups. Just plain simple code that checks if "I" have access to write to a folder.

mahendra - mahendra_moni NOSPAM-AT-NOSPAM yahoo.com Report As Irrelevant  
Written: 5/16/2006 10:33 PM
Ho to verify the file or directory permission in c#

mahendra - mahendra_moni NOSPAM-AT-NOSPAM yahoo.com Report As Irrelevant  
Written: 5/16/2006 10:36 PM
Give article is very helpful to set permission but i want to verify file or directory permission so please tell me about that.

Malathi - mals52002 NOSPAM-AT-NOSPAM yahoo.com Report As Irrelevant  
Written: 10/28/2006 2:10 AM
Can u tell us how to implement the same functionality in .net version 1.1 asap

Wunna - whlaing NOSPAM-AT-NOSPAM gmail.com Report As Irrelevant  
Written: 12/15/2006 5:59 PM
This Article nearly can solve our problem. My problem is we cannot Strict the NTFS permission while program is running because we use Automation with DCOM objects. So after execution, I can strict the folder. But it is not complete soultion for us. While running, I want to grant access only for one Application not for all. Is that possiable?

Anonymous User - abolfazl_javan NOSPAM-AT-NOSPAM yahoo.com Report As Irrelevant  
Written: 10/10/2008 4:44 AM
hi. i want to set folder visibility permission for each user in active directory. Can u send me a sample to do this. E-Mail address: Abolfazl_Javan@yahoo.com thanks. byTe.

Anonymous User Report As Irrelevant  
Written: 1/15/2009 10:24 AM
Is the ADsSecurity 2.5 Type Library still relevant? I cannot find it at the MS website stated above.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please