我使用以下代碼與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);
}
}
更新:我將其配置爲不再保存到註冊表中,而是保存到開始菜單 - >啓動文件夾。這效果更好。 – 2011-03-17 20:02:46
任何機會,你可以PM我的代碼,我有完全相同的問題,我很高興你已經知道了這一點。 – Cilvic 2011-05-26 13:09:09
@Cilvic繼續,在techerator dot com的evan處給我發一封電子郵件,我可以引導你完成整個過程。這不是太複雜。 – 2011-05-26 15:34:46