获取当前ClickOnce的应用程序发布者名称?

Jua*_*uan 7 .net c# clickonce

是否可以读取当前运行的ClickOnce应用程序(Project Properties -> Publish -> Options -> Publisher name在Visual Studio中设置的应用程序)的发布者名称?

为什么我需要它的原因是作为描述的运行当前运行的应用程序的另一个实例文章,向它传递参数.

当然我知道我的应用程序的发布者名称,但是如果我硬编码,稍后我决定更改我的发布者名称,我很可能会忘记更新这段代码.

Jua*_*uan 5

这是另一种选择.请注意,它只会获取当前正在运行的应用程序的发布者名称,这就是我所需要的.

我不确定这是解析XML的最安全的方法.

public static string GetPublisher()
{
    XDocument xDocument;
    using (MemoryStream memoryStream = new MemoryStream(AppDomain.CurrentDomain.ActivationContext.DeploymentManifestBytes))
    using (XmlTextReader xmlTextReader = new XmlTextReader(memoryStream))
    {
        xDocument = XDocument.Load(xmlTextReader);
    }
    var description = xDocument.Root.Elements().Where(e => e.Name.LocalName == "description").First();
    var publisher = description.Attributes().Where(a => a.Name.LocalName == "publisher").First();
    return publisher.Value;
}
Run Code Online (Sandbox Code Playgroud)


cod*_*ion 1

您可能认为这很简单,但我在框架中没有看到任何内容可以为您提供此信息。

如果您想要破解,您可以从注册表获取发布者。

免责声明- 代码丑陋且未经测试......

    ...
    var publisher = GetPublisher("My App Name");
    ...

    public static string GetPublisher(string application)
    {
        using (var key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall"))
        {
            var appKey = key.GetSubKeyNames().FirstOrDefault(x => GetValue(key, x, "DisplayName") == application);
            if (appKey == null) { return null; }
            return GetValue(key, appKey, "Publisher");
        }
    }

    private static string GetValue(RegistryKey key, string app, string value)
    {
        using (var subKey = key.OpenSubKey(app))
        {
            if (!subKey.GetValueNames().Contains(value)) { return null; }
            return subKey.GetValue(value).ToString();
        }
    }
Run Code Online (Sandbox Code Playgroud)

如果您找到更好的解决方案,请继续关注。