c#从安全选项卡中删除用户

时间:2012-09-19 15:08:53

标签: c#

我正在删除从文件夹中移除权限给我希望能够从文件夹安全选项卡列表中删除用户的给定用户

摘录代码

myDirectorySecurity.RemoveAccessRule(new FileSystemAccessRule(User, FileSystemRights.Write | FileSystemRights.Read, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
     PropagationFlags.None, AccessControlType.Allow));

                myDirectoryInfo.SetAccessControl(myDirectorySecurity);

我希望在删除权限后删除用户..

picture http://i93.photobucket.com/albums/l66/reavenm/Capture_zps51403cae.png

我尝试将其添加到我的代码

DataSet dataSet = new DataSet(); 
string sql = "SELECT key, fdate, user, perm, sfolder FROM  permuser WHERE        fdate=CURDATE()";

MySqlConnection connection = new MySqlConnection(MyConString);
MySqlCommand cmdSel = new MySqlCommand(sql, connection);
new MySqlDataAdapter(cmdSel).Fill(dataSet, "permuser");


foreach (DataRow row in dataSet.Tables["permuser"].Rows)
    {
         string fuser = row["user"].ToString();
         string pathtxt = row["sfolder"].ToString();

        DirectoryInfo myDirectoryInfo = new DirectoryInfo(pathtxt);
        DirectorySecurity myDirectorySecurity = myDirectoryInfo.GetAccessControl();
        string User = System.Environment.UserDomainName + "\\" + fuser;


                myDirectorySecurity.RemoveAccessRule(new FileSystemAccessRule(User, FileSystemRights.Write | FileSystemRights.Read, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
     PropagationFlags.None, AccessControlType.Allow));

                myDirectoryInfo.SetAccessControl(myDirectorySecurity);
                   //it should go here
    }

connection.Close();
connection.Dispose();
}


catch (MySqlException ex)
{
Console.WriteLine(ex.Message);
Environment.Exit(0);
}

1 个答案:

答案 0 :(得分:1)

我们使用以下内容从他们无法再访问的文件夹中删除用户/组。

var accountToRemove = "Some account";
var security = Directory.GetAccessControl(path);
var rules = security.GetAccessRules(true, true, typeof(NTAccount));

foreach (FileSystemAccessRule rule in rules)
{
    if (rule.IdentityReference.Value == accountToRemove) 
        security.RemoveAccessRuleSpecific(rule);

}

用以下

替换你的foreach循环
foreach (DataRow row in dataSet.Tables["permuser"].Rows)
{
     string fuser = row["user"].ToString();
     string pathtxt = row["sfolder"].ToString();

    DirectoryInfo myDirectoryInfo = new DirectoryInfo(pathtxt);
    DirectorySecurity myDirectorySecurity = myDirectoryInfo.GetAccessControl();
    string User = System.Environment.UserDomainName + "\\" + fuser;

    var rules = myDirectorySecurity.GetAccessRules(true, true, typeof(NTAccount));
    foreach( var rule in rules)
    {
        if (rule.IdentityReference.Value == User) 
            security.RemoveAccessRuleSpecific(rule);

    }
}