2009-12-26 108 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