在 Vista 上设置文件夹权限
我正在尝试在 Vista 计算机上设置文件夹及其所有子文件夹的权限。 到目前为止我的代码是这样的。
public static void SetPermissions(string dir)
{
DirectoryInfo info = new DirectoryInfo(dir);
DirectorySecurity ds = info.GetAccessControl();
ds.AddAccessRule(new FileSystemAccessRule(@"BUILTIN\Users",
FileSystemRights.FullControl,
InheritanceFlags.ContainerInherit,
PropagationFlags.None,
AccessControlType.Allow));
info.SetAccessControl(ds);
}
然而它并没有像我期望的那样工作。
即使我以管理员身份运行代码,它也不会设置权限。
我正在使用的文件夹位于 C:\ProgramData\<我的文件夹> 中 我可以手动更改它的权限。
任何人都想为我指明正确的方向。
I am trying to set the permissions of a folder and all of it's children on a vista computer. The code I have so far is this.
public static void SetPermissions(string dir)
{
DirectoryInfo info = new DirectoryInfo(dir);
DirectorySecurity ds = info.GetAccessControl();
ds.AddAccessRule(new FileSystemAccessRule(@"BUILTIN\Users",
FileSystemRights.FullControl,
InheritanceFlags.ContainerInherit,
PropagationFlags.None,
AccessControlType.Allow));
info.SetAccessControl(ds);
}
However it's not working as I would expect it to.
Even if I run the code as administrator it will not set the permissions.
The folder I am working with is located in C:\ProgramData\<my folder> and I can manually change the rights on it just fine.
Any one want to point me in the right direction.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以答案有两个。 首先,在对文件夹设置权限之前创建了一个子文件夹,我需要在权限上添加一个或多个标志来创建子文件夹,以便文件夹和文件都继承权限。
之后一切似乎都正常。
So the answer is two fold. First off a sub folder was being created before the permissions were set on the folder and I needed to or in one more flag on the permissions to make it so both folders and files inherited the permissions.
After that every thing appears to be working.
这可能是一个愚蠢的问题,但是您是否尝试过手动执行相同的操作(例如使用资源管理器)? Vista 有一些目录,即使是管理员组中的用户也无法在不采取其他步骤的情况下对其进行修改。 我认为您首先需要采取两个步骤。
首先,使用资源管理器进行与您尝试在代码中执行的相同修改。 如果失败,请排除故障。
其次,在您自己的用户文件夹下创建的目录中测试您的代码。 您不需要管理员权限即可执行此操作; 登录帐户应该能够更改 c:\Users\yourname\documents 下文件夹的 ACL。
我还将单步执行调试器中的代码,并在调用 SetAccessControl 之前查看“ds”对象。 这可能会向你展示一些意想不到的东西,让你走上正确的道路。
This may be a dumb question, but have you tried performing the same action manually (e.g. using Explorer)? Vista has some directories that not even users in the Administrators group can modify without taking additional steps. I think there are two steps you need to take first.
First, use Explorer to make the same modification you're trying to do in your code. If it fails, troubleshoot that.
Second, test your code on a directory you created under your own user folder. You shouldn't need admin privs to do that; the logged-in account should be able to change ACL on folders under e.g. c:\Users\yourname\documents.
I'd also step through the code in the debugger and look at the "ds" object just before your call to SetAccessControl. That might show you something unexpected to set you on the right path.