Paradox _ Max
Paradox _ Max

Reputation: 43

WPF displaying Session username in a text

I am working on a project using WPF where I have to display the computer's username as a text string in WPF.

I know we use %username% cmd command to retrieve the session username however how to use it in as a string in a WPF application?

Thank you

Upvotes: 1

Views: 247

Answers (1)

SpaceBoy
SpaceBoy

Reputation: 130

First of all you need to install System.DirectoryServices.AccountManagement NuGet Package.

Then you have declare a string in your .cs file:

private string UsernameText = Environment.Username;
public string UsernameText
{
   get => UsernameText;
   set 
   {
      if (UsernameText != value)
      {
         UsernameText = value;
      }
   }
}

and in your .xaml file:

<TextBlock Text="{Binding Path=SessionUsernameText, Mode=TwoWay}"/>

Upvotes: 1

Related Questions