5 October 2011

Adding read permission for groups and users with files

If you wanna put some permissions for groups or users who doesn’t need to access certain files, you can use the class FileSystemAccessRule.

 

Let see the example:

 

using System;

using System.IO;

using System.Security.AccessControl;

 

namespace FileSystemExample

{

    class FileExample

    {

        public static void Main()

        {

            try

            {

                string fileName = "test.xml";

 

                Console.WriteLine("Adding access control entry for "

                    + fileName);

 

                // Add the access control entry to the file.

                AddFileSecurity(fileName, @"DomainName\AccountName",

                    FileSystemRights.ReadData, AccessControlType.Allow);

 

                Console.WriteLine("Removing access control entry from "

                    + fileName);

 

                // Remove the access control entry from the file.

                RemoveFileSecurity(fileName, @"DomainName\AccountName",

                    FileSystemRights.ReadData, AccessControlType.Allow);

 

                Console.WriteLine("Done.");

            }

            catch (Exception e)

            {

                Console.WriteLine(e);

            }

        }

 

        // Adds an ACL entry on the specified file for the specified account.

        public static void AddFileSecurity(string fileName, string account,

            FileSystemRights rights, AccessControlType controlType)

        {

 

 

            // Get a FileSecurity object that represents the

            // current security settings.

            FileSecurity fSecurity = File.GetAccessControl(fileName);

 

            // Add the FileSystemAccessRule to the security settings.

            fSecurity.AddAccessRule(new FileSystemAccessRule(account,

                rights, controlType));

 

            // Set the new access settings.

            File.SetAccessControl(fileName, fSecurity);

 

        }

 

        // Removes an ACL entry on the specified file for the specified account.

        public static void RemoveFileSecurity(string fileName, string account,

            FileSystemRights rights, AccessControlType controlType)

        {

 

            // Get a FileSecurity object that represents the

            // current security settings.

            FileSecurity fSecurity = File.GetAccessControl(fileName);

 

            // Remove the FileSystemAccessRule from the security settings.

            fSecurity.RemoveAccessRule(new FileSystemAccessRule(account,

                rights, controlType));

 

            // Set the new access settings.

            File.SetAccessControl(fileName, fSecurity);

 

        }

    }

}

 

Link: http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.filesystemaccessrule.aspx

 

0 comentários: