繁体   English   中英

获取有关在VSTO AddIn中Office应用程序中记录的LiveId(Office 365)帐户的信息

[英]Get information about LiveId (Office 365) account logged in Office application in VSTO AddIn

我为Word,Excel等开发了VSTO加载项。我需要获取有关当前登录Office应用程序的用户的信息。 我至少需要一个电子邮件地址。

在此输入图像描述

我发现了这些属性Globals.ThisAddIn.Application.UserName.UserInitials.UserAddress 但它不是关于LiveID帐户。 这是关于办公室用户设置。

我如何获得所需信息?

我找到了只有一种方法来检索这些信息 - 阅读注册表...如果它是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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM