我为 Word、Excel 等开发 VSTO 插件。我需要获取有关当前登录 Office 应用程序的用户的信息。我至少需要一个电子邮件地址。
我找到了这些属性Globals.ThisAddIn.Application.UserName,.UserInitials并且.UserAddress。但这与LiveID帐户无关。这是关于办公室用户设置的。
我怎样才能获得所需的信息?
我为 Word、Excel 等开发 VSTO 插件。我需要获取有关当前登录 Office 应用程序的用户的信息。我至少需要一个电子邮件地址。
我找到了这些属性Globals.ThisAddIn.Application.UserName,.UserInitials并且.UserAddress。但这与LiveID帐户无关。这是关于办公室用户设置的。
我怎样才能获得所需的信息?
我发现只有一种方法可以检索此信息 - 阅读注册表...HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\16.0\Common\Identity\Identities\
如果是 Office 2016,则有键。有子键,如xxxxx_LiveIdwherexxxxx与ProviderId值匹配。
您至少可以EmailAddress从该子项中读取值。
所以我写了一些 C# 代码来检索登录 LiveID 用户的电子邮件地址:
string GetUserEmailFromOffice365()
{
string Version = "16.0"; //TODO get from AddIn
string identitySubKey = $@"Software\Microsoft\Office\{Version}\Common\Identity\Identities";
using (var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(identitySubKey))
{
if (key != null && key.SubKeyCount > 0)
{
foreach (var subkeyName in key.GetSubKeyNames())
{
using (var subkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey($@"{identitySubKey}\{subkeyName}"))
{
object value = null;
try
{
value = subkey.GetValue("EmailAddress");
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
if (value != null && value is string)
{
return value as string;
}
}
}
}
}
return null;
}
当然,您不应该对Version值进行硬编码。Globals.ThisAddIn.Application.Version您可以从方法中的ThisAddIn.cs文件中获取并记住 Office 版本ThisAddIn_Startup。