C# 获取有关在VSTO加载项中登录到Office应用程序的LiveId(Office 365)帐户的信息
我为Word、Excel等开发了VSF插件。 我需要获得有关当前登录Office应用程序的用户的信息。 我至少需要一个电子邮件地址 我找到了这些属性C# 获取有关在VSTO加载项中登录到Office应用程序的LiveId(Office 365)帐户的信息,c#,ms-office,vsto,office365,windows-live-id,C#,Ms Office,Vsto,Office365,Windows Live Id,我为Word、Excel等开发了VSF插件。 我需要获得有关当前登录Office应用程序的用户的信息。 我至少需要一个电子邮件地址 我找到了这些属性Globals.ThisAddIn.Application.UserName,UserInitials和UserAddress。但这不是关于LiveIDaccount的。它是关于office用户设置的 如何获取所需信息?我只找到了一种检索此信息的方法-读取注册表。。。 有密钥HKEY\U CURRENT\U USER\SOFTWARE\Micros
Globals.ThisAddIn.Application.UserName
,UserInitials
和UserAddress
。但这不是关于LiveID
account的。它是关于office用户设置的
如何获取所需信息?我只找到了一种检索此信息的方法-读取注册表。。。 有密钥
HKEY\U CURRENT\U USER\SOFTWARE\Microsoft\Office\16.0\Common\Identity\Identifications\
如果是Office 2016。有一些子键,如xxxxx\u 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;
}
当然,您不应该硬编码
Version
value。您可以从ThisAddIn.cs中的Globals.ThisAddIn.Application.version
文件中的ThisAddIn.cs
方法获取并记住office版本。我只找到了一种检索此信息的方法-读取注册表。。。
有密钥HKEY\U CURRENT\U USER\SOFTWARE\Microsoft\Office\16.0\Common\Identity\Identifications\
如果是Office 2016。有一些子键,如xxxxx\u 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;
}
当然,您不应该硬编码Version
value。您可以从ThisAddIn.cs中的Globals.ThisAddIn.Application.version
中的ThisAddIn.cs
文件中获取并记住office版本