背景:我编写的代码将输出所有当前登录的用户的 Active Directory 组名。我希望通过 IdentityReference.Translate 获得组名(例如:Acomp_user_BIG),而不是通过 IdentityReference.Value 返回当前用户的 SID(例如:S-1-5-32-544)。
这是我使用的代码:
Public ReadOnly Property Groups As IdentityReferenceCollection
Get
Dim irc As IdentityReferenceCollection
Dim ir As IdentityReference
irc = WindowsIdentity.GetCurrent().Groups
For Each ir In irc
Dim account As IdentityReference = ir.Translate(GetType(NTAccount))
Debug.WriteLine(account.Value)
Next
End Get
End Property
不幸的是,我在“End Get”中收到错误:
Warning 1 Property 'Groups' doesn't return a value on all code paths.
A null reference exception could occur at run time when the
result is used.
修复此错误的任何建议?