Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF在文本中显示会话用户名_C#_.net_Wpf_Cmd - Fatal编程技术网

C# WPF在文本中显示会话用户名

C# WPF在文本中显示会话用户名,c#,.net,wpf,cmd,C#,.net,Wpf,Cmd,我正在做一个使用WPF的项目,我必须在WPF中将计算机的用户名显示为文本字符串 我知道我们使用%username%cmd命令来检索会话用户名,但是如何在WPF应用程序中将其作为字符串使用 谢谢首先,您需要安装System.DirectoryServices.AccountManagement NuGet软件包 然后在.cs文件中声明一个字符串: private string UsernameText = Environment.Username; public string UsernameTe

我正在做一个使用WPF的项目,我必须在WPF中将计算机的用户名显示为文本字符串

我知道我们使用%username%cmd命令来检索会话用户名,但是如何在WPF应用程序中将其作为字符串使用


谢谢

首先,您需要安装System.DirectoryServices.AccountManagement NuGet软件包

然后在.cs文件中声明一个字符串:

private string UsernameText = Environment.Username;
public string UsernameText
{
   get => UsernameText;
   set 
   {
      if (UsernameText != value)
      {
         UsernameText = value;
      }
   }
}
<TextBlock Text="{Binding Path=SessionUsernameText, Mode=TwoWay}"/>
.xaml文件中:

private string UsernameText = Environment.Username;
public string UsernameText
{
   get => UsernameText;
   set 
   {
      if (UsernameText != value)
      {
         UsernameText = value;
      }
   }
}
<TextBlock Text="{Binding Path=SessionUsernameText, Mode=TwoWay}"/>

或者,您可以安装System.Security.Principal.Windows NuGet软件包并使用:私有字符串SessionUsernameText=System.Security.Principal.WindowsIdentity.GetCurrent().Name.Split('\\')[1]);