我指的是 C# - How to get current user picture但我从来没有找到解决办法。 (我正在使用 Win7 操作系统)
对于某些用户,图片位于
C:\Users\UserName\AppData\Local\Temp\UserName.bmp
(其中 UserName 是用户昵称)对于其他用户此路径抛出 FileNotFoundException 但图片存在。
在哪里可以找到有关路径或真实图片的信息?是否有包含此信息的注册表?
最佳答案
这blog post显示如何设置用户磁贴(图片)。在接近尾声的评论中(Michael Anthony,4 月 10 日,22:45),评论者描述了如何获取图片。我已将信息收集到 C# 片段中。请记住,这是基于未记录的 Windows Shell 函数。
using System;
using System.Text;
using System.Drawing;
[DllImport("shell32.dll", EntryPoint = "#261",
CharSet = CharSet.Unicode, PreserveSig = false)]
public static extern void GetUserTilePath(
string username,
UInt32 whatever, // 0x80000000
StringBuilder picpath, int maxLength);
public static string GetUserTilePath(string username)
{ // username: use null for current user
var sb = new StringBuilder(1000);
GetUserTilePath(username, 0x80000000, sb, sb.Capacity);
return sb.ToString();
}
public static Image GetUserTile(string username)
{
return Image.FromFile(GetUserTilePath(username));
}
请注意,此 Shell 函数会创建文件\Users\
此外,我已经在 Win7 上进行了测试。我不知道它与以前的 Windows 版本的兼容性。
归功于 Joco和 Michael Anthony .
关于C#获取用户头像(头像),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7253833/