2012-01-10 44 views
7

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

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

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

+0

在启动的ClickOnce应用程序,我会用尝试使用激活的网址与查询字符串参数,而不是如果你的环境中可能你的其他实例的条款(需要部署到Web服务器)。按照http://stackoverflow.com/questions/7588608/clickonce-application-wont-accept-command-line-arguments/7588781#7588781 – Reddog 2012-01-11 02:20:58

+0

我不知道这是否适用于我的情况。据我所知,这从服务器安装,但不从服务器“运行”。我的意思是你可以在安装完毕后离线运行它。你只是应该将该URL传递给'Process.Start'? – Juan 2012-01-11 02:35:26

回答

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

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

0

我不知道有关ClickOnce,但通常情况下,你可以使用的System.Reflection框架阅读组装-信息:

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

不幸的是,世界上没有“出版人”自定义属性,只是扔了这一点作为可能的解决方法

4

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

我不确定这是否是解析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; 
} 
相关问题