C# 返回ClickOnce版本不会';从Windows注册表启动应用程序时无法工作

C# 返回ClickOnce版本不会';从Windows注册表启动应用程序时无法工作,c#,.net,registry,version,C#,.net,Registry,Version,我将以下代码用于System.Deployment以返回.NET 3.5 C#应用程序的ClickOnce版本: public string version { get { System.Reflection.Assembly _assemblyInfo = System.Reflection.Assembly.GetExecutingAssembly(); string ourVersion = string.Empty; if

我将以下代码用于
System.Deployment
以返回.NET 3.5 C#应用程序的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;
    }
}
如果我正常启动应用程序(例如,从“开始”菜单),则始终正确返回该值。但是,如果我使用注册表项使用Windows自动启动应用程序,应用程序将返回默认的硬编码值1.0.0.0

如果关闭自动启动的应用程序并手动重新打开,它将再次返回正确的ClickOnce版本号

你知道为什么会这样吗?下面是我用来设置注册表项的代码:

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

公共类Util
{
private const string RUN_LOCATION=@“Software\Microsoft\Windows\CurrentVersion\RUN”;
/// 
///设置部件的“自动启动”值。
/// 
///注册表项名称
///程序集位置(例如,Assembly.getExecutionGassembly().location)
公共静态void SetAutoStart(字符串键名、字符串assemblyLocation)
{
RegistryKey key=Registry.CurrentUser.CreateSubKey(运行位置);
key.SetValue(keyName,assemblyLocation);
}
/// 
///返回是否启用自动启动。
/// 
///注册表项名称
///程序集位置(例如,Assembly.getExecutionGassembly().location)
publicstaticbool是自动启动的(stringkeyname,stringassemblyLocation)
{
RegistryKey key=Registry.CurrentUser.OpenSubKey(运行位置);
if(key==null)
返回false;
字符串值=(字符串)key.GetValue(keyName);
如果(值==null)
返回false;
返回值(值==assemblyLocation);
}
/// 
///取消设置程序集的自动启动值。
/// 
///注册表项名称
公共静态无效自动启动(字符串键名)
{
RegistryKey key=Registry.CurrentUser.CreateSubKey(运行位置);
key.DeleteValue(keyName);
}
}

我已经隔离了这个问题。我不会假装完全理解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";

然后我将其与以前的代码结合起来,在注册表中设置该位置。

Update:我将其配置为不再保存到注册表,而是保存到“开始”菜单-->启动文件夹。这样效果更好。如果你有机会给我发那段代码,我也有同样的问题,我很高兴你已经解决了。@Cilvic继续,给techerator.com的evan发一封电子邮件,我可以指导你完成这个过程。这并不太复杂。感谢你的提议,我决定坚持使用.appref ms的注册表项,因为启动文件夹解决方案中的快捷方式显然存在vista自动更新的问题。
string allProgramsPath = Environment.GetFolderPath(Environment.SpecialFolder.Programs);
string shortcutPath = Path.Combine(allProgramsPath, keyName);
shortcutPath = Path.Combine(shortcutPath, keyName) + ".appref-ms";