C#-列出Active Directory中的所有用户帐户时出错
我正在尝试使用Windows窗体列出Active Directory中所有基于用户的帐户的一些基本详细信息(带有一个自定义图标,取决于帐户是否启用/禁用)。在运行时,我得到了这个初始错误,剩下的是第二个屏幕截图: 出于某种原因,图标位于第1列而不是第4列,我只拥有一个帐户。我的代码:C#-列出Active Directory中的所有用户帐户时出错,c#,winforms,active-directory,C#,Winforms,Active Directory,我正在尝试使用Windows窗体列出Active Directory中所有基于用户的帐户的一些基本详细信息(带有一个自定义图标,取决于帐户是否启用/禁用)。在运行时,我得到了这个初始错误,剩下的是第二个屏幕截图: 出于某种原因,图标位于第1列而不是第4列,我只拥有一个帐户。我的代码: try { var domainName = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().Domai
try
{
var domainName = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName;
MessageBox.Show(domainName);
using (var context = new PrincipalContext(ContextType.Domain, domainName))
{
using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
{
foreach (var result in searcher.FindAll())
{
ListViewItem tmpItem = null;
DirectoryEntry child = result.GetUnderlyingObject() as DirectoryEntry;
switch (child.SchemaClassName)
{
case "user":
int flag = (int)child.Properties["userAccountControl"].Value;
bool disabled = Convert.ToBoolean(flag & 0x0002);
if (disabled == false)
{
tmpItem = new ListViewItem(new string[] {
(string)child.Properties["givenName"].Value,
(string)child.Properties["sn"].Value,
(string)child.Properties["samAccountName"].Value
}, (int)AdImages.User);
this.listView_ad.Items.Add(tmpItem);
}
else
{
tmpItem = new ListViewItem(new string[] {
(string)child.Properties["givenName"].Value,
(string)child.Properties["sn"].Value,
(string)child.Properties["samAccountName"].Value
}, (int)AdImages.block);
this.listView_ad.Items.Add(tmpItem);
}
break;
case "organizationalUnit":
break;
case "container":
break;
case "computer":
break;
case "group":
break;
default:
break;
}
}
}
}
}
catch (Exception e)
{
MessageBox.Show("ERROR : " + e, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
编辑:已删除尝试/捕获以获取完整错误:
************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
at ComputeamPasswordManager.PasswordManager.getUsers()
at ComputeamPasswordManager.PasswordManager.PasswordManager_Load(Object sender, EventArgs e)
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
编辑2:在域设备上运行的Visual Studio的屏幕截图
尝试更改此选项(以及类似的表达式):
像这样:
(string)child.Properties["givenName"]?.Value ?? ""
我所需要做的就是以管理员的身份运行VisualStudio 感谢您的建议,结果与上面我的原始代码完全相同。然后,您可能需要使用调试器逐步检查代码并找到引发错误的确切行。您还可以(暂时)删除try/catch,因为这会隐藏错误的真实位置。如果在Visual Studio中运行此操作,您还应该能够准确地看到哪行代码抛出此错误。这和错误消息本身一样重要。你不需要在一台特殊的机器上。只要用户有足够的权限,任何加入域的计算机都应该工作。@Rawns这意味着
child。属性[“userAccountControl”]
会导致null
,检查null
引用上的属性(即:Value
)会导致异常。您必须准备好处理那里的null
结果。
(string)child.Properties["givenName"]?.Value ?? ""