i m trying to grant permission to a folder using c#. but can't. i can restrict the folder with following code. plz can any one help me?
//**************** CODE *******************
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.Allow));
myDirectoryInfo.SetAccessControl(myDirectorySecurity);
MessageBox.Show("Permissions Altered Successfully" + User);
}
}
}