[英]Get information about LiveId (Office 365) account logged in Office application in VSTO AddIn
我找到了只有一种方法来检索这些信息 - 阅读注册表...如果它是Office 2016,则有HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Office\\16.0\\Common\\Identity\\Identities\\
键。有xxxxx_LiveId
这样的子项,其中xxxxx
匹配到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;
}
对于cource,您不应该对Version
值进行硬编码。 您可以通过ThisAddIn_Startup
方法中的ThisAddIn.cs
文件中的Globals.ThisAddIn.Application.Version
获取并记住Office版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.