public static UserSettings GetUserPreferences()
 {
     IsolatedStorageFileStream fileStream = null;
     try
     {
         var fileLocation = IsolatedStorageFile.GetUserStoreForApplication();
         if (fileLocation.FileExists(FileSystem.SettingsFile))
         {
             fileStream = new IsolatedStorageFileStream(FileSystem.SettingsFile, FileMode.Open, fileLocation);
             var serializer = new XmlSerializer(typeof(UserSettings));
             var userSettings = (UserSettings)serializer.Deserialize(fileStream);
             if (userSettings != null)
             {
                 return userSettings;
             }
             else
             {
                 throw new Exception("Unable lo load user preferences");
             }
         }
         else
         {
             var settings = new UserSettings();
             UpdateUserSettings(settings);
             return settings;
         }
     }
     finally
     {
         if (fileStream!=null)
         {
             fileStream.Close();
         }
     }
 }
 /// <summary>
 /// Updates or creates a new settings file
 /// </summary>
 /// <param name="settings">The user settings object</param>
 public static void UpdateUserSettings(UserSettings settings)
 {
     IsolatedStorageFileStream fileStream=null;
     try
     {
         fileStream = new IsolatedStorageFileStream(FileSystem.SettingsFile, FileMode.Create, IsolatedStorageFile.GetUserStoreForApplication());
         var serializer = new XmlSerializer(typeof(UserSettings));
         serializer.Serialize(fileStream, settings);
     }
     finally
     {
         if (fileStream != null)
         {
             fileStream.Close();
         }
     }
 }
        /// <summary>
        /// Retrieves list of recent birthdays
        /// </summary>
        /// <returns>List of friend</returns>
        private void GetRecentBirthdays()
        {
            try
            {
                var friendList = BirthdayUtility.GetFriendList();
                var recentBirthdays = new List<FriendEntity>();
                var todayBirthday = new List<FriendEntity>();
                var weekBirthday = new List<FriendEntity>();
                Settings = SettingsUtility.GetUserPreferences();
                LoadLocalizedRes();
                var notificationCount = 0;
                var isReminderExpired = false;
                foreach (var friend in friendList)
                {
                    if (!friend.Birthday.HasValue || (friend.IsHidden.HasValue && friend.IsHidden.Value)) continue;

                    var daysAhead = DateTimeUtility.GetTimeToEvent(friend.Birthday.Value);

                    //if the birthday is today, add recent birthdays to list
                    if (daysAhead == 0)
                    {
                        //if the birthday is today, raise a toast notification
                        recentBirthdays.Add(friend);
                        todayBirthday.Add(friend);
                        notificationCount++;

                        if (friend.LastToastRaisedYear < DateTime.Now.Year)
                        {
                            if (!Settings.ReminderNotification.LocalNotifications.HasValue || Settings.ReminderNotification.LocalNotifications.Value)
                            {
                                RaiseToast(friend.Name + " " + Resources.BdayTodayMsg, Resources.BdayTileLabel, new Uri("/FriendDetails.xaml?" + UriParameter.FriendId + "=" + friend.UniqueId, UriKind.Relative));
                            }

                            //update the last notification raised year
                            friend.LastToastRaisedYear = DateTime.Now.Year;
                            BirthdayUtility.UpdateFriendDetails(friend);
                        }
                    }

                    //if birthday is tomorrow
                    if (daysAhead == 1)
                    {
                        recentBirthdays.Add(friend);
                        notificationCount++;
                    }

                    //check if reminder for upcoming birthdays exist
                    if (daysAhead > 0 && daysAhead < 7)
                    {
                        if (!friend.IsReminderCreated)
                        {
                            isReminderExpired = true;
                            notificationCount++;
                        }
                        weekBirthday.Add(friend);
                    }
                }

                //raise a toast notification if reminders have to be created
                if (isReminderExpired)
                {
                    RaiseToast(Resources.UpdateBdayReminder, Resources.BdayTileLabel, new Uri("/StartChecks.xaml?" + UriParameter.Action + "=Toast", UriKind.Relative));
                }

                //update shell tile
                UpdateTile(notificationCount, weekBirthday);

                BirthdayUtility.DownloadProfileImages("No");

                //send an email - [[to be implemented later]]
                //if (settings.ReminderNotification.SendEmailReminders && !string.IsNullOrEmpty(settings.UserDetails.Email))
                //{
                //    String emailText = getReminderEmailContent(todayBirthday);
                //    sendEmail(settings.UserDetails.Email,emailText);
                //}

                ////wish via email
                //if (todayBirthday!=null && todayBirthday.Count>0)
                //{
                //    wishViaEmail(todayBirthday);
                //}
            }
            catch (Exception ex)
            {
                AppLog.WriteToLog(DateTime.Now, ex.Message + ". " + ex.StackTrace, LogLevel.Error);
                throw;
            }
        }
        // Code to execute when the application is launching (eg, from Start)
        // This code will not execute when the application is reactivated
        private void Application_Launching(object sender, LaunchingEventArgs e)
        {
            try
            {
                CheckLicense();
                //Remove existing agent and re register
                recycleBackAgent();

                //load user preferences or create it
                UserPreferences = SettingsUtility.GetUserPreferences();
            }
            catch (InvalidOperationException invalidException)
            {
                AppLaunchError = getAppLaunchError(invalidException);
            }
            catch (Exception ex)
            {
                AppLaunchError = ex.Message;
            }
        }