简体   繁体   中英

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

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}"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM