限制用户使用C#删除文件

时间:2016-08-01 11:28:48

标签: c# asp.net winforms file restriction

大家好我想在我的应用程序中创建一个功能,以便用户不能删除桌面上的文件夹和图标中的文件...到目前为止,我能做的是用户无法打开一个文件夹,但当我给FileSystemRights删除然后它不工作...我怎么能实现这一点,请帮助我....

这是我的代码..

     public string p = @"D:\\test";
     public string SysLogUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

    public void FreezeFile(string path, string user)
    {


        System.IO.DirectoryInfo folderinfo = new System.IO.DirectoryInfo(path);
        DirectorySecurity foldersecurity = folderinfo.GetAccessControl();
        FileSystemAccessRule rule = new FileSystemAccessRule(user, FileSystemRights.ReadData, InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Deny);

        foldersecurity.AddAccessRule(rule);
        Directory.SetAccessControl(path, foldersecurity);
    }
    private void btn_Action_Click(object sender, EventArgs e)
    {
        this.FreezeFile(p,SysLogUser);
        MessageBox.Show("Permissions Altered Successfully");
    }

0 个答案:

没有答案