I'm using the following code with System.Deployment
to return the ClickOnce version of my .NET 3.5 C# application:
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;
}
}
If I launch the application normally (from the Start menu, for example), this value is always returned correctly. However, if I start the application automatically with Windows using a registry key, the application returns the default hardcoded value of 1.0.0.0.
If I close the automatically-started application and re-open it manually, it then returns the correct ClickOnce version number again.
Any ideas on why this might be? Here is the code I'm using to set the registry key:
string keyName = "MyApp";
string assemblyLocation = Assembly.GetExecutingAssembly().Location;
Util.SetAutoStart(keyName, assemblyLocation);
And
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);
}
}