How do I detect the first time a ClickOnce-deployed application has been run?

房东的猫 提交于 2019-12-17 19:22:24

问题


I have a ClickOnce-deployed application and I'm currently using this to detect the first time a new deployment is being run:

if (ApplicationDeployment.IsNetworkDeployed
    && ApplicationDeployment.CurrentDeployment.IsFirstRun)
{
    // Display release notes so user knows what's new
}

It seems to works as expected after ClickOnce performs an automatic update.

But it doesn't work at all when the user goes to publish.htm on the install site and installs a newer version manually. Is there a way to detect both of these conditions reliably?

Edit: The situation I'm trying to account for: sometimes users hear that an update has been released and manually go to publish.htm to get the new version, instead of launching the application and letting ClickOnce handle the upgrade. To ClickOnce, this is apparently indistinguishable from a first-time install. Is that true?

Solution Code: I ended up creating a ClickOnce helper class with the following key section:

    public static bool IsFirstRun
    {
        get
        {
            if (!IsNetworkDeployed)
                return false; // not applicable == bool default value

            if (!File.Exists(VersionFileName))
                return true;

            return (GetLastRunVersion() != Version.ToString());
        }
    }

    public static void StoreCurrentVersion()
    {
        File.WriteAllText(VersionFileName, Version.ToString());
    }

    public static string GetLastRunVersion()
    {
        using (var stream = File.OpenText(VersionFileName))
        {
            return stream.ReadToEnd();
        }
    }

    public static string VersionFileName
    {
        get
        {
            StringBuilder filename = new StringBuilder(Files.LocalFilesPath);
            if (!filename.ToString().EndsWith(@"\"))
                filename.Append(@"\");
            filename.Append(@"versioninfo.dat");
            return filename.ToString();
        }
    }

回答1:


Include an extra file in your ClickOnce install called justInstalled.txt (or something). Chedk for that file when the app starts. If you find it delete it and run any code for your first run of that deployment. The file will stay missing until the next deployment/upgrade.




回答2:


Yes the IsFirstRun is reset for each program version...

This is the except from Microsoft:

The value of this property is reset whenever the user upgrades from one version to the next. If you want to perform an operation only the very first time any version of the application is run, you will need to perform an additional test, such as checking for the existence of a file you created the first time, or storing a flag using Application Settings.

which more or less answers you question...




回答3:


if (ApplicationDeployment.IsNetworkDeployed)
{
    ApplicationDeployment deployment = ApplicationDeployment.CurrentDeployment;

    if (deployment.IsFirstRun)
    {
        //Your Code Here....
    }
} 

See IsFirstRun Doc



来源:https://stackoverflow.com/questions/5811780/how-do-i-detect-the-first-time-a-clickonce-deployed-application-has-been-run

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!