如何获取当前登录Windows的用户的图像帐户?

问题描述:

在网上搜索我发现这个代码UWP,得到的图像,但我不能在WPF中包含命名空间Windows.Storage。我试图包含所有可能的引用,但不起作用。我想有一个解决方案WPF包括Windows.Storage(如果可能),或者干脆另一种方式来实现代码来获得图像如何获取当前登录Windows的用户的图像帐户?

StorageFile image = UserInformation.GetAccountPicture(AccountPictureKind.SmallImage) as StorageFile; 
     if (image != null) 
     { 
      rootPage.NotifyUser("SmallImage path = " + image.Path, NotifyType.StatusMessage); 
     } 
     else 
     { 
      rootPage.NotifyUser("Small Account Picture is not available", NotifyType.StatusMessage); 
      mediaPlayer.Visibility = Visibility.Collapsed; 
      smallImage.Visibility = Visibility.Collapsed; 
      largeImage.Visibility = Visibility.Collapsed; 

}

+0

此代码片段似乎适用于UWP。 – mm8

+0

我认为他们可能存储在'C:\ Users \ \ AppData \ Roaming \ Microsoft \ Windows \ AccountPictures' – itsme86

+0

@ itsme86谢谢,但在文件夹中我可以找到用于帐户的所有照片。我应该得到目前使用的照片。 –

Windows.​System.​User​Profile命名空间是UWP应用开发和不适WPF。

如果将应用程序打包(转换)为UWP:https://docs.microsoft.com/sv-se/windows/uwp/porting/desktop-to-uwp-supported-api#apis-supported-in-both-converted-apps-and-desktop-applications,则可能可以使用它。请注意,Windows 10或更高版本不支持UserInformation类。相反,你应该使用User类。

在.NET应用程序,你可以尝试搜索C:\Users\<user>\AppData\Roaming\Microsoft\Windows\Account Pictures文件夹中的最新修改的文​​件:

How to find the most recent file in a directory using .NET, and without looping?

+0

获得图像非常感谢。我认为最简单的解决方案是在文件夹中找到最新的修改文件 –

基于this answer如果你看一看here

只要你拥有当前登录的用户名,您应该可以访问它。至少在Win OS 7中,我不知道你正在使用哪个操作系统。