背景:我编写了将输出所有当前登录的用户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.

有任何解决此错误的建议吗?

最佳答案

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

         **return irc**
    End Get
End Property

但是,通过查看代码可以修改irc,因此您应该创建一个新的IdentityReferenceCollection(假设它具有.add()方法),然后执行.add(account)
并返回您的新收藏

10-08 11:55