2011-03-16 68 views
0

我使用以下代碼與System.Deployment一起返回我的.NET 3.5 C#應用程序的ClickOnce版本:從Windows註冊表啓動啓動應用程序時,返回ClickOnce版本不起作用

public string version 
{ 
    get 
    { 
     System.Reflection.Assembly _assemblyInfo = System.Reflection.Assembly.GetExecutingAssembly(); 
     string ourVersion = string.Empty; 

     if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed) 
     { 
      ourVersion = System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString(); 
     } 
     else 
     { 
      if (_assemblyInfo != null) 
      { 
       ourVersion = _assemblyInfo.GetName().Version.ToString(); 
      } 
     } 
     return ourVersion; 
    } 
} 

如果我正常啓動應用程序(例如,從「開始」菜單),此值始終爲正確返回。但是,如果我使用註冊表項自動啓動應用程序,則應用程序將返回默認的硬編碼值1.0.0.0。

如果我關閉自動啓動的應用程序並手動重新打開它,它會再次返回正確的ClickOnce版本號。

關於爲什麼會出現這種情況的任何想法?這是我用來設置註冊表項的代碼:

string keyName = "MyApp"; 
string assemblyLocation = Assembly.GetExecutingAssembly().Location; 
Util.SetAutoStart(keyName, assemblyLocation); 

public class Util 
{ 
    private const string RUN_LOCATION = @"Software\Microsoft\Windows\CurrentVersion\Run"; 

    /// <summary> 
    /// Sets the autostart value for the assembly. 
    /// </summary> 
    /// <param name="keyName">Registry Key Name</param> 
    /// <param name="assemblyLocation">Assembly location (e.g. Assembly.GetExecutingAssembly().Location)</param> 
    public static void SetAutoStart(string keyName, string assemblyLocation) 
    { 
     RegistryKey key = Registry.CurrentUser.CreateSubKey(RUN_LOCATION); 
     key.SetValue(keyName, assemblyLocation); 
    } 

    /// <summary> 
    /// Returns whether auto start is enabled. 
    /// </summary> 
    /// <param name="keyName">Registry Key Name</param> 
    /// <param name="assemblyLocation">Assembly location (e.g. Assembly.GetExecutingAssembly().Location)</param> 
    public static bool IsAutoStartEnabled(string keyName, string assemblyLocation) 
    { 
     RegistryKey key = Registry.CurrentUser.OpenSubKey(RUN_LOCATION); 
     if (key == null) 
      return false; 

     string value = (string)key.GetValue(keyName); 
     if (value == null) 
      return false; 

     return (value == assemblyLocation); 
    } 

    /// <summary> 
    /// Unsets the autostart value for the assembly. 
    /// </summary> 
    /// <param name="keyName">Registry Key Name</param> 
    public static void UnSetAutoStart(string keyName) 
    { 
     RegistryKey key = Registry.CurrentUser.CreateSubKey(RUN_LOCATION); 
     key.DeleteValue(keyName); 
    } 
} 

回答

2

我已經分離出的問題。我不會假裝正確地理解 ClickOnce是如何工作的,但基本上,如果直接啓動可執行文件,它將不會以「ClickOnce模式」運行。這意味着它不會檢查更新並且不會得到正確的版本號(因爲它實際上沒有網絡部署)。

到目前爲止,我發現的最佳解決方案是指向ClickOnce .appref-ms文件而不是.exe文件。這個文件就像是一個排序的快捷方式,並且位於開始菜單中。

下面是我用得到我的應用程序的.appref-ms文件的位置代碼:

string allProgramsPath = Environment.GetFolderPath(Environment.SpecialFolder.Programs); 
string shortcutPath = Path.Combine(allProgramsPath, keyName); 
shortcutPath = Path.Combine(shortcutPath, keyName) + ".appref-ms"; 

然後,我結合起來,與我以前的代碼來設置在註冊表中的位置。

+0

更新:我將其配置爲不再保存到註冊表中,而是保存到開始菜單 - >啓動文件夾。這效果更好。 – 2011-03-17 20:02:46

+0

任何機會,你可以PM我的代碼,我有完全相同的問題,我很高興你已經知道了這一點。 – Cilvic 2011-05-26 13:09:09

+0

@Cilvic繼續,在techerator dot com的evan處給我發一封電子郵件,我可以引導你完成整個過程。這不是太複雜。 – 2011-05-26 15:34:46

相關問題