我创建了一个应用程序,该应用程序将拒绝对文件夹的所有完全控制访问权限,直到某人使用正确的密码登录到应用程序。锁定程序的来源是:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.AccessControl;
using System.DirectoryServices;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string Directoryd = "D:";
string mydirectory = Directoryd + "\\" + "rs\\";
if (!Directory.Exists(mydirectory))
{
Directory.CreateDirectory(mydirectory);
}
DirectoryInfo di = new DirectoryInfo(mydirectory);
DirectorySecurity ds = di.GetAccessControl();
ds.AddAccessRule(new FileSystemAccessRule(
@"everyone",
FileSystemRights.FullControl,
AccessControlType.Deny));
di.SetAccessControl(ds);
}
}
}
这将拒绝对文件夹D的所有访问:/ rs / 我想到了一个脚本,可以按下这样的按钮来解锁文件夹:
private void login_Click(object sender, EventArgs e)
{
string username = text1.Text;
string password = text2.Text;
if(username == "aname" && password == "password")
{
string Directoryd = "D:";
string mydirectory = Directoryd + "\\" + "rs\\";
if (!Directory.Exists(mydirectory))
{
Directory.CreateDirectory(mydirectory);
}
DirectoryInfo di = new DirectoryInfo(mydirectory);
DirectorySecurity ds = di.GetAccessControl();
ds.AddAccessRule(new FileSystemAccessRule(
@"everyone",
FileSystemRights.FullControl,
AccessControlType.Allow));
di.SetAccessControl(ds);
}
}
但这显示访问被拒绝错误。如何避免这个错误?
答案 0 :(得分:2)
拒绝权限的优先级高于允许权限。您必须删除拒绝权限,而不是添加另一个权限。
答案 1 :(得分:0)
尝试替换它:
ds.AddAccessRule(new FileSystemAccessRule(@"everyone", FileSystemRights.FullControl, AccessControlType.Allow));
di.SetAccessControl(ds);
与
ds.AddAccessRule(new FileSystemAccessRule(@"everyone", FileSystemRights.FullControl, AccessControlType.Deny));
di.RemoveAccessRule(ds);
希望这会有所帮助。