Folder Permissions c#

Hi, I've got some code to change the folder permissions.
The code will add the user, but when I check the permissions they haven't changed.
The following is the code.
Can anyone help, thanks.
I had to add the references System.Management and System.Management.Instrumentation from .NET controls.
I got the code from 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

   using System.IO;

   using System.Security.AccessControl;

   using System.Management;

  using System.Management.Instrumentation;
namespace Permissions
    public partial class Form1 : Form
        public Form1()


        private void Form1_Load(object sender, EventArgs e)

        private void SelectDirectorybtn_Click(object sender, EventArgs e)


                // creating a new instance fot FolderBrowsingDialog 

                                    //to provide user capability to select target Folder

                FolderBrowserDialog myFolderBrowserDialog = new FolderBrowserDialog();


                // showing dialog



                // Show the path of selected directory in our text Box 

               textBox1.Text = myFolderBrowserDialog.SelectedPath.ToString();

           private void Permissionbtn_Click(object sender, EventArgs e)


       public void GetUsers()


             // This query will query for all user account names in our current Domain

             SelectQuery sQuery = new SelectQuery("Win32_UserAccount", "Domain='" 

                             + System.Environment.UserDomainName.ToString() + "'");




                        // Searching for available Users

                         ManagementObjectSearcher mSearcher = new 



                        foreach (ManagementObject mObject in mSearcher.Get())


                                // Adding all user names in our combobox




             catch (Exception ex)





       private void Permissionbtn_Click_1(object sender, EventArgs e)
           // retrieving the directory information

           DirectoryInfo myDirectoryInfo = new DirectoryInfo(textBox1.Text);

           // Get a DirectorySecurity object that represents the 

           // current security settings.

           DirectorySecurity myDirectorySecurity = myDirectoryInfo.GetAccessControl();

           string User = System.Environment.UserDomainName + "\\" +


           // Add the FileSystemAccessRule to the security settings. 

           // FileSystemRights is a big list we are current using Read property but you 

           // can alter any other or many sme of which are:

           // Create Directories: for sub directories Authority

           // Create Files: for files creation access in a particular folder
           // Delete: for deletion athority on folder

           // Delete Subdirectories and files: for authority of deletion over 

           //subdirectories and files

           // Execute file: For execution accessibility in folder

           // Modify: For folder modification

           // Read: For directory opening

           // Write: to add things in directory

           // Full Control: For administration rights etc etc

           // Also AccessControlType which are of two kinds either “Allow” or “Deny” 

           //myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(User,

           //                                FileSystemRights.Read, AccessControlType.Allow));
           //myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(User,

           //                                           FileSystemRights.Write, AccessControlType.Allow));

           myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(User,

                                                      FileSystemRights.FullControl, AccessControlType.Allow));

           // Set the new access settings. 


           // Showing a Succesfully Done Message

           MessageBox.Show("Permissions Altered Successfully");


Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
Are you running this program as an Administrator from an account that has Admin privileges?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Netlink2Author Commented:
good point, yes I am
Netlink2Author Commented:
The program actually works. The full control is under special permissions. I wasn't looking there. But your answer prompted me to look at that, so I'll give you the points.
Dave BaldwinFixer of ProblemsCommented:
Thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.