none
Changing Folder Permission using C# RRS feed

  • Question

  • Hi,

        I want to change folder permission from "Read " to "Write "/"Read " , I am working on IIS root folder on which resides on "C:\Inetpub\ftproot " . By default this folder comes with Read permission, but I am automating this using application, for which I need to enable Write permission for this folder by which clients connected to this FTP server can put there files.

      I tried few examples in MSDN, I am not still successful in enabling permission for the folder, I am working on a network domain , and the sample application provided works on "Domain Name" & "User Name" basis.

      I would like to resolve this issue, If you can provide me some pointers on this issue, it would be a great help for me to proceed ahead with project.  I would like to know if this folder permission can be enabled without using Domain & User Name parameters, I am afraid if this is the logic the application may file to work on a single system which is independent from network domains!

     I have attached my code below , I scanned my Domain Name and User Name and used in code , the code executed but write permission was not enabled.

    using System;
    using System.IO;
    using System.Security.AccessControl;
    
    namespace FileSystemExample
    {
        class DirectoryExample
        {
            public static void Main()
            {
                try
                {
                    string DirectoryName = @"C:\Inetpub\ftproot";
    
                    Console.WriteLine("Adding access control entry for " + DirectoryName);
                    // Add the access control entry to the directory.
                    AddDirectorySecurity(DirectoryName, @"MYDOMAIN\MyAccount", FileSystemRights.Write, AccessControlType.Allow);
                    Console.WriteLine("Removing access control entry from " + DirectoryName);
                    // Remove the access control entry from the directory.
                    RemoveDirectorySecurity(DirectoryName, @"MYDOMAIN\MyAccount", FileSystemRights.Write, AccessControlType.Allow);
                    Console.WriteLine("Done.");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
    
                Console.ReadLine();
            }
    
            // Adds an ACL entry on the specified directory for the specified account.
            public static void AddDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
            {
                // Create a new DirectoryInfo object.
                DirectoryInfo dInfo = new DirectoryInfo(FileName);
    
                // Get a DirectorySecurity object that represents the 
                // current security settings.
                DirectorySecurity dSecurity = dInfo.GetAccessControl();
    
                // Add the FileSystemAccessRule to the security settings. 
                dSecurity.AddAccessRule(new FileSystemAccessRule(Account,
                Rights,
                ControlType));
    
                // Set the new access settings.
                dInfo.SetAccessControl(dSecurity);
    
            }
    
            // Removes an ACL entry on the specified directory for the specified account.
            public static void RemoveDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
            {
                // Create a new DirectoryInfo object.
                DirectoryInfo dInfo = new DirectoryInfo(FileName);
    
                // Get a DirectorySecurity object that represents the 
                // current security settings.
                DirectorySecurity dSecurity = dInfo.GetAccessControl();
    
                // Add the FileSystemAccessRule to the security settings. 
                dSecurity.RemoveAccessRule(new FileSystemAccessRule(Account,
                Rights,
                ControlType));
    
                // Set the new access settings.
                dInfo.SetAccessControl(dSecurity);
    
            }
        }
    }

    Monday, October 12, 2009 4:36 AM

Answers