Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

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

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();
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...