2012-01-10 111 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; 
} 
相關問題