2009-12-26 102 views
0

嗨,大家好,我正在编写一个应用程序,它检索从foreach循环中填充的每个NT组的组名和访问权限。另外,我还包含了一个DataGridView控件,其中每个单元格都有一个复选框列,应用程序将相应地检查每个单元格,例如每个组的读,写,修改等。我不能为我的生活,找出如何相应地检查这些盒子。下面的代码片段演示了我正在尝试使用标准的DataGridView控件文本框列,但是我想让这些复选框而不是文本框。任何反馈将不胜感激。在下面的代码片段中,Property是从另一个方法传入的路径。DataGridView复选框问题

private void CheckDirPermissions(ResultProperty Property) 
    { 
     if (Property.Type == typeof(string) && !Property.IsArray) 
     { 
      try 
      { 
       FileSecurity folderSecurity = File.GetAccessControl(Property.String); 
       foreach (FileSystemAccessRule fileSystemAccessRule in folderSecurity.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount))) 
       { 



        string IdentityReference = fileSystemAccessRule.IdentityReference.ToString(); 
        string AccessControlType = fileSystemAccessRule.AccessControlType.ToString(); 
        string filesystemrights = fileSystemAccessRule.FileSystemRights.ToString(); 
        string IsInherited = fileSystemAccessRule.IsInherited.ToString(); 




        DataGridDirPermissions.Rows.Add(IdentityReference, 
                filesystemrights,          
                AccessControlType, 
                IsInherited); 

       } 
      } 
      catch (Exception) 
      { 
       MessageBox.Show("Path does not exist.", "Path Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      } 
     } 
     else return; 
    } 

回答

1

您必须定义类型为DataGridViewCheckBoxColumn的DataGridView的读,写,修改等列。然后,当你读的权限为一组,你可以得到每个权限对应的布尔值,并用这些值创建行:

foreach (FileSystemAccessRule fileSystemAccessRule in folderSecurity.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount))) 
{ 
    string IdentityReference = fileSystemAccessRule.IdentityReference.ToString(); 

    AccessControlType accessControlType = fileSystemAccessRule.AccessControlType; 
    FileSystemRights filesystemrights = fileSystemAccessRule.FileSystemRights; 
    bool isInherited = fileSystemAccessRule.IsInherited; 

    // .. Get specific permissions ... 

    bool allowControlType = accessControlType == AccessControlType.Allow; 
    bool canRead = (filesystemrights & FileSystemRights.Read) == FileSystemRights.Read; 
    bool canWrite = (filesystemrights & FileSystemRights.Write) == FileSystemRights.Write; 
    bool canExecute = (filesystemrights & FileSystemRights.ExecuteFile) == FileSystemRights.ExecuteFile; 

    // ... Any more specific permissions ... 

    dataGridView1.Rows.Add(IdentityReference, allowControlType, canRead, canWrite, canExecute, ...); 
} 

这样,你的DataGridView将有组名的第一个单元格(作为一个TextBox)对于每一个特定权限的复选框,如:

Everyone     (check)  (check)  (no check) (no check) 
BUILTIN\Administrators  (check)  (check)  (check)  (check) 
BUILTIN\Users    (check)  (check)  (check)  (no check) 

等等...

+0

这完美的作品。非常感谢Alexphi – Sanch01R 2009-12-30 14:22:18