Link to home
Start Free TrialLog in
Avatar of Netlink2
Netlink2

asked on

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.
P.S.
I had to add the references System.Management and System.Management.Instrumentation from .NET controls.
I got the code from http://www.redmondpie.com/applying-permissions-on-any-windows-folder-using-c/ 
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()
        {
            InitializeComponent();


              GetUsers();
        }

        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

                myFolderBrowserDialog.ShowDialog();

      

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

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

    }
        //http://www.redmondpie.com/applying-permissions-on-any-windows-folder-using-c/
           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() + "'");

      

              try

              {

                        // Searching for available Users

                         ManagementObjectSearcher mSearcher = new 

                                                        ManagementObjectSearcher(sQuery);

     

                        foreach (ManagementObject mObject in mSearcher.Get())

                        {

                                // Adding all user names in our combobox

                                comboBox1.Items.Add(mObject["Name"]);

                        }

             }

             catch (Exception ex)

             {

                        MessageBox.Show(ex.ToString());

             }

    }

       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 + "\\" +

                                                      comboBox1.SelectedItem.ToString();



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

           myDirectoryInfo.SetAccessControl(myDirectorySecurity);



           // Showing a Succesfully Done Message

           MessageBox.Show("Permissions Altered Successfully");

       }
}
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Netlink2
Netlink2

ASKER

good point, yes I am
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.
Thank you.