如何在 WPF 中获取 Windows 用户名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16273659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:46:46  来源:igfitidea点击:

how to Get Windows Username in WPF

wpf

提问by Ujjwal27

I would like to display the Username in my WPF application, where it should get the username from the current user logged in?

我想在我的 WPF 应用程序中显示用户名,它应该从当前登录的用户那里获取用户名?

so my question how can we get the username of windows logged in user from WPF App ?

所以我的问题是如何从 WPF App 获取登录 Windows 的用户名?

回答by Michel Keijzers

System.Security.Principal.WindowsIdentity.GetCurrent().Name

I also found:

我还发现:

Environment.UserName

or

或者

System.Windows.Forms.SystemInformation.UserName

I cannot try it so check for yourself the result.

我无法尝试,所以请自己检查结果。

Added: Full user name:

补充:完整用户名:

Imports System.DirectoryServices.AccountManagement

Dim userFullName As String = UserPrincipal.Current.DisplayName

回答by Mehdi Bugnard

Call WindowsIdentity.GetCurrent()to get the Windows user identity.

调用WindowsIdentity.GetCurrent()以获取 Windows 用户身份。

You can get the name from this.

你可以从中得到这个名字。