使用C#在Win7中设置一个坚不可摧的文件夹

时间:2012-04-11 16:57:05

标签: c# permissions directory

我在设置文件夹权限时遇到了一些麻烦。我可以设置读权限和权限,但是当我设置删除权限时,用户仍然可以删除该文件夹。

这是我的代码:

public void SetFolderUndestructable(String path)
    {
        SelectQuery sQuery = new SelectQuery("Win32_UserAccount", "Domain='" + System.Environment.UserDomainName.ToString() + "'");
        try
        {
            ManagementObjectSearcher mSearcher = new ManagementObjectSearcher(sQuery);
            DirectoryInfo dInfor = new DirectoryInfo(path);
            DirectorySecurity dSecurity = dInfor.GetAccessControl();
            foreach (ManagementObject mObject in mSearcher.Get())
            {
                string User = System.Environment.UserDomainName.ToString()+"\\"+mObject["name"].ToString();
                if (mObject["name"].ToString() != System.Environment.UserName)
                {
                    dSecurity.AddAccessRule(new FileSystemAccessRule(User, FileSystemRights.Delete, AccessControlType.Deny));
                }
                dInfor.SetAccessControl(dSecurity);
            }
        }
        catch (Exception x)
        {
            MessageBox.Show(x.ToString());
        }
    }

有什么想法吗? 感谢

0 个答案:

没有答案