Checking First-Run Info in Windows Phone

There are a lot of reasons that you’d want to know if your app has been run before.  Maybe you want to ask the user if you can collect usage information.  Maybe you want to set up the default settings.  Whatever the reason, it’s often useful to know.  A simple method would be this:

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
bool? firstrun;
settings.TryGetValue("FirstRun", out firstrun);
if (firstrun ?? true)
    settings["Parameter1"] = "default";
    settings["Parameter2"] = 4;
    // ETC

    firstrun = false;
    settings["FirstRun"] = firstrun;

This’ll go in a method called from your Application_Launching() InitializePhoneApplication() method via a dispatcher:

// Default InitializePhoneApplication() method plus dispatcher:
private void InitializePhoneApplication()
    if (phoneApplicationInitialized)

    // Create the frame but don't set it as RootVisual yet; this allows the splash
    // screen to remain active until the application is ready to render.
    RootFrame = new PhoneApplicationFrame();
    RootFrame.Navigated += CompleteInitializePhoneApplication;
    RootFrame.Dispatcher.BeginInvoke(BuildDefaultSettings); // Or whatever you called it

    // Handle navigation failures
    RootFrame.NavigationFailed += RootFrame_NavigationFailed;

    // Ensure we don't initialize again
    phoneApplicationInitialized = true;

Remember to have a using reference to System.IO.IsolatedStorage.

Here’s the thing: we can do better.  If we store the version, we can see if the app has just been upgraded, too:

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
Version version;
Version current = (new AssemblyName(Assembly.GetExecutingAssembly().FullName)).Version;

if (!settings.TryGetValue("Version", out version))
    settings["Version"] = current;
    settings["Parameter1"] = "default";
    settings["Parameter2"] = 4;
else if (version.CompareTo(current) > 0)
    if (!settings.Contains("Parameter1"))
        settings["Parameter1"] = "default";
    if (!settings.Contains("Parameter2"))
        settings["Parameter2"] = 4;
    settings["Version"] = current;

In case you’re wondering, we can’t get the version via Assembly.GetExecutingAssembly().GetName().Version because GetName() is a security critical method, and we’re not allowed to use security critical code in Windows Phone apps.

This code lets us figure out if the app has run before and if it was updated since we last used it.  the call to settings.TryGetValue() will return false if it can’t find anything.  In this case, we just want to populate the settings with default values.

If it does find a stored Version, we compare it to the current version.  CompareTo() will return something greater than 0 if the running app is newer than the stored value, 0 if it’s the same version, and something less than 0 if the user has, through time travel, managed to get an old version.  If this is a new version, we have to check for each setting before putting in the default, as it’s not nice to overwrite the user’s choices.  If the version hasn’t changed, there’s nothing to do (unless you want to make a note somewhere, but that’s you).  If time travel is happening, I can’t really help you.


Thursday, September 1st, 2011 Thoughts

10 Comments to Checking First-Run Info in Windows Phone

  • Gianguido says:

    Thanks a lot, I'll use this trick in my app!

  • Great app trick. Really love it.

  • Rock says:

    I must try this trick in my app. Thank your very much for this. I visit again your site to see more and I hope you will come with more useful updates in future.

  • Ayoub says:

    thanks for sharing

  • Shidouka says:

    Once you've logged in with your Google account, just access the Google Play Store as you would on a mobile device and install the apps you want to try. There are also quick links to some of the most popular apps on the home screen to save you a few clicks…. Read more

  • Tim says:

    Your articles and blogs are inspirational.Brad

  • pes 2018 apk says:

    great piece of article. thanks for sharing this great information.

  • apkpot says:

    cool post keep it up your nice work.

  • apkshouse says:

    Admiring time and energy you put into your website and detailed information you present. It’s great to come across a blog every once in a while that isn’t the same old rehashed information. Great work keeps it up.

  • Leave a Reply