如何设置文件夹权限
如何创建/编辑/添加特定文件夹的文件夹权限?本地C盘有一个名为“test”的文件夹,如何使用C#设置该文件夹的权限?
我已经编写了一些代码:
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根本不会被继承。我错过了什么吗?或者有人可以指导我如何继承该文件夹的权限?
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);
}
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果继承意味着所有子对象都获得相同的权限,则需要将 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.
在我看来,您只是缺少此标志,您可以在参数列表中使用按位运算符将其加入:
InheritanceFlags.ObjectInherit
还有更多详细信息,包括指向具有权限和设置矩阵的 Google 电子表格的链接,在此线:
使用 set-acl 和 powershell 设置继承和传播标志< /a>
希望这有帮助...
It looks to me like you are just missing this flag, which you would join using the bitwise operator in your parameter list:
InheritanceFlags.ObjectInherit
There are more details, including a link to a Google Spreadsheet with a matrix of permissions and settings, in this thread:
Setting Inheritance and Propagation flags with set-acl and powershell
Hope this helps...