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

时间:2012-01-10 06:29:07

标签: c# .net clickonce

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

我需要它的原因是运行当前正在运行的应用程序的另一个实例,如this文章中所述,并将参数传递给它。

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

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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();
        }
    }

如果您找到更好的解决方案,请跟进。

答案 2 :(得分:0)

我不了解ClickOnce,但通常情况下,您可以使用System.Reflection框架阅读assembly-info:

public string AssemblyCompany
    {
        get
        {
            object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
            if (attributes.Length == 0)
            {
                return "";
            }
            return ((AssemblyCompanyAttribute)attributes[0]).Company;
        }
    }

不幸的是,没有“发布者”自定义属性,只是将其作为一种可能的解决方法

相关问题