Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.5
Hi,
 
I'm using the following code to restrict a folder in c#, but can't grant permission to access the folder. Can anyone help me please?
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.AccessControl;
using System.Management;
using System.Management.Instrumentation;
using System.Windows.Forms;
 
namespace PermissionToFolder
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            getusers();
        }
 
        public void getusers()
        {
            SelectQuery squery = new SelectQuery("Win32_UserAccount", "Domain='"+ System.Environment.UserDomainName.ToString() + "'");
            try
            {
                ManagementObjectSearcher msearchar = new ManagementObjectSearcher(squery);
                //ManagementObject mobject = new ManagementObject();
                foreach (ManagementObject mobject in msearchar.Get())
                {
                    cmbPermission.Items.Add(mobject["Name"]);
                }
            }
            catch (Exception e) { MessageBox.Show(e.ToString()); }
        }
 
        private void btnDirectory_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.ShowDialog();
            txtDirectory.Text = fbd.SelectedPath.ToString();
        }
 
        private void btnPermission_Click(object sender, EventArgs e)
        {
            DirectoryInfo myDirectoryInfo = new DirectoryInfo(txtDirectory.Text);
 
            DirectorySecurity myDirectorySecurity = myDirectoryInfo.GetAccessControl();
            string User = System.Environment.UserDomainName + "\\" + cmbPermission.SelectedItem.ToString();
            myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(User, FileSystemRights.Read, AccessControlType.Deny));
            myDirectoryInfo.SetAccessControl(myDirectorySecurity);
            MessageBox.Show("Permissions Altered Successfully" + User);
        }
    }
}
Posted 24-Dec-10 4:43am
nhjewel493
Edited 24-Dec-10 4:52am
JF201553.1K
v2
Comments
JF2015 at 24-Dec-10 10:53am
   
Edited to add code formatting.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try running your app as administrator.

  Permalink  
v2
Comments
Marcus Kramer at 24-Dec-10 11:40am
   
Good answer, John, good answer!!! (In a Family Feud style response)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 528
1 OriginalGriff 459
2 ChintanShukla 305
3 Richard Deeming 250
4 RyanDev 230
0 Sergey Alexandrovich Kryukov 8,901
1 OriginalGriff 7,571
2 CPallini 2,603
3 Richard MacCutchan 2,095
4 Abhinav S 1,893


Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 8 Mar 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100