I want to create a textblock that says "Welcome username".
username = current Windows user.
I'm working in a WPF project.
I want to create a textblock that says "Welcome username".
username = current Windows user.
I'm working in a WPF project.
You should use Environment.UserName
(msdn):
Gets the user name of the person who is currently logged on to the Windows operating system.
Example:
Console.WriteLine("UserName: {0}", Environment.UserName);
If you want to get the user who is currently running the process, use:
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
OR
string userName = System.Environment.UserName
But the user you get from Environment.UserName
is not guaranteed to be the user running the current process.
The difference between the two is explained in this comment:
The reason to prefer
WindowsIdentity.GetCurrent()
is that this returns the account that the application is running as. This is not necessarily the account that is currently logged into Windows (think "RunAs" or impersonation/delegation). So, if you want to know the logged-in user, use the Environment (if you trust it). If you want to know the security context your application is running as, usingWindowsIdentity.GetCurrent()
.