2017-10-12 11 views
0

コードのこの小さな作品は私がコンピュータにLANに接続したときにアカウント管理をゲット - 表示名 - Vb.net

Imports System.DirectoryServices.AccountManagement 

Dim userFullName As String = UserPrincipal.Current.DisplayName 
Label1.Text = "Hi " & userFullName & ", Welcome !!" 

上記のコードは正常に動作し、ユーザーの表示名でログインして、システムの現在を取得します。 LANが取り外され、WIFIが接続されていると動作しません。誰かがこれに対する回避策を導くことができますか?

+0

これはのみ限り、ディレクトリサーバーを接触させることができるように機能
ユーザーを作成しますがcachedDisplaynameという名前で、次のメソッドを使用した設定スコープ。私はWIFI経由でディレクトリサーバーに到達できないと思います。 – MatSnow

+0

私はLANに接続せずにdisplaynameを得ることができる任意の回避策? –

答えて

0

この方法は、ディレクトリサーバーに接続できる場合にのみ機能します。
それ以外の場合はPrincipalServerDownExceptionとなります。

回避策として、サーバーにアクセス可能な状態でdisplaynameをキャッシュすることができます。

たとえば、My.Settingsの内部にキャッシュすることができます。ラベルテキストを設定する

Function GetUserDisplayName() As String 
    Dim userFullName As String 

    Try 
     'Reading the displayname from the directory 
     userFullName = UserPrincipal.Current.DisplayName 
     'Saving the displayname in My.Settings 
     My.Settings.cachedDisplayname = userFullName 
     My.Settings.Save() 
    Catch ex As PrincipalServerDownException 
     If String.IsNullOrWhiteSpace(My.Settings.cachedDisplayname) Then 
      'displayname has not been cached yet, use Username as compromise solution 
      userFullName = Environment.UserName 
     Else 
      'read the cached displayname from My.Settings 
      userFullName = My.Settings.cachedDisplayname 
     End If 
    End Try 

    Return userFullName 
End Function 

Label1.Text = String.Format("{0}, Welcome !!", GetUserDisplayName()) 
関連する問題