この方法は、ディレクトリサーバーに接続できる場合にのみ機能します。
それ以外の場合は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())
これはのみ限り、ディレクトリサーバーを接触させることができるように機能
ユーザーを作成しますが
cachedDisplayname
という名前で、次のメソッドを使用した設定スコープ。私はWIFI経由でディレクトリサーバーに到達できないと思います。 – MatSnow私はLANに接続せずにdisplaynameを得ることができる任意の回避策? –