Get current ClickOnce's application publisher name?

廉价感情. 提交于 2019-12-08 17:30:00

问题


Is it possible to read the publisher name of the currently running ClickOnce application (the one you set at Project Properties -> Publish -> Options -> Publisher name in Visual Studio)?

The reason why I need it is to run another instance of the currently running application as described in this article and pass parameters to it.

Of course I do know my application's publisher name, but if I hard code it and later on I decide to change my publisher's name I will most likely forget to update this piece of code.


回答1:


You would think this would be trivial, but I don't see anything in the framework that gives you this info.

If you want a hack, you can get the publisher from the registry.

Disclaimer - Code is ugly and untested...

    ...
    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();
        }
    }

If you find a better solution, please follow-up.




回答2:


Here is another option. Note that it will only get the publisher name for the currently running application, which is all I need.

I'm not sure if this is the safest way to parse the 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;
}



回答3:


I dont know about ClickOnce, but normally, you can read the assembly-info using the System.Reflection framework:

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

Unfortunately, theres no "publisher" custom-attribute, just throwing this out as a possible work-around



来源:https://stackoverflow.com/questions/8799409/get-current-clickonces-application-publisher-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!