2012-09-19 58 views
1

我从一个文件夹中删除删除权限来我希望能够从文件夹的安全选项卡列表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); 
} 
+0

更换您的foreach循环。如果你没有做它在C#中,有一个命令行实用程序可以使用。谷歌“icacls”(或“cacls”为旧版本的Windows) – Wug

回答

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 (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); 

    } 
} 
+0

这不会删除用户表单活动目录?,只是从指定的文件夹? –

+0

我试图添加到我的代码无济于事,显然取代变量。 –

+0

我会推荐打印出文件系统认为现有的用户是什么。然后,您可以使您的输入符合预期。 (用户:{0}“,rule.IdentityReference.Value) –