Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current ClickOnce's application publisher name?

Tags:

c#

.net

clickonce

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.

like image 757
Juan Avatar asked Jan 10 '12 06:01

Juan


2 Answers

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;
}
like image 83
Juan Avatar answered Nov 12 '22 04:11

Juan


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.

like image 43
codeConcussion Avatar answered Nov 12 '22 05:11

codeConcussion