问题描述
如何创建/编辑/添加文件夹的权限,以特定的文件夹? ?有一种称为本地磁盘C.我如何使用C#设置权限到该文件夹test的文件夹
How can I create/edit/add folder permission to specific folder? There is a folder called "test" in local disk C. How do I set permission to that folder using C#?
我写的已经有一些代码:
I wrote some code already:
public void getusers()
{
SelectQuery squery = new SelectQuery("Win32_UserAccount", "Domain='" + System.Environment.UserDomainName.ToString() + "'");
try
{
ManagementObjectSearcher msearchar = new ManagementObjectSearcher(squery);
foreach (ManagementObject mobject in msearchar.Get())
{
comboBox1.Items.Add(mobject["Name"]);
}
}
catch (Exception e) { MessageBox.Show(e.ToString()); }
}
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.ShowDialog();
textBox1.Text = fbd.SelectedPath.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
DirectoryInfo myDirectoryInfo = new DirectoryInfo(textBox1.Text);
DirectorySecurity myDirectorySecurity = myDirectoryInfo.GetAccessControl();
string User = System.Environment.UserDomainName + "\\" + comboBox1.SelectedItem.ToString();
myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(User, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow));
//myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(User, FileSystemRights.Write, InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow));
myDirectoryInfo.SetAccessControl(myDirectorySecurity);
MessageBox.Show("Permissions Altered Successfully" + User);
}
此代码已经成功地将用户添加到该文件夹,但permissionIi设置为文件夹完全不继承。我错过了什么?或者,可能有人指导我如何继承权限的文件夹?
This code already successfully adds the User to the folder but the permissionIi set to that folder is not inherited at all. Did I miss something? Or could someone guide me how to inherit the permission to that folder?
推荐答案
如果通过继承你的意思是所有子对象收到相同的权限,您需要将您的PropagationFlags设置为InheritOnly。另外,如果你想你的文件是为了匹配规则集的权限,更改为InheritanceFlags ObjectInherit。尝试使用下面这条线。
If by inherited you mean that all child objects receive the same permissions, you will need to set your PropagationFlags to InheritOnly. Further if you want your files to also match the permission of the ruleset, change your InheritanceFlags to ObjectInherit. try using this line below.
myDirectoryInfo.AddAccessRule(new FileSystemAccessRule(User, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow));
这篇关于如何设置文件夹的权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!