总体而言还算简单,只需要以下代码:
var userName = Environment.UserName;
Console.WriteLine(userName);
以下方法需要调用Win32的DLL,因此支持的站点仅为Windows
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;
using System.Runtime.InteropServices;
using System.Text;
public static partial class UserTileHelper
{
[DllImport("shell32.dll", EntryPoint = "#261",
CharSet = CharSet.Unicode, PreserveSig = false)]
public static extern void GetUserTilePath(string? username, uint whatever, StringBuilder picpath, int maxLength);
public static string GetUserTilePath(string? username = null)
{
var sb = new StringBuilder(1000);
GetUserTilePath(username, 0x80000000, sb, sb.Capacity);
return sb.ToString();
}
public static ImageSource GetUserTile(string? username = null) => new BitmapImage(new Uri(GetUserTilePath(username)));
}
然后使用的时候只需要调用以下代码:
var userTile = UserTileHelper.GetUserTile();