/// <summary> /// Loads Last State on start if it exists /// </summary> /// <returns> true if successful</returns> public bool LoadPreviousPMState(out object x) { MeterStateFile meterStateFile = new MeterStateFile(); string meterStateFileJson = null; try { using (IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()) { if (!isolatedStorageFile.DirectoryExists("CarLocatorSystems")) { isolatedStorageFile.CreateDirectory("CarLocatorSystems"); }//end if IsolatedStorageFileStream fileStream = null; try { fileStream = isolatedStorageFile.OpenFile(@"CarLocatorSystems/MeterState.dat", FileMode.OpenOrCreate); using (StreamReader streamReader = new StreamReader(fileStream)) { fileStream = null; meterStateFileJson = streamReader.ReadToEnd(); if (meterStateFileJson.Length == 0) { x = meterStateFile; return(false); } } }//end using 2 finally { if (fileStream != null) { fileStream.Dispose(); } } }//end using 1 x = JsonConvert.DeserializeObject <MeterStateFile>(meterStateFileJson); return(true); //<= Loaded with Info } catch (Exception e) { MessageBox.Show(e.Message, "Error", MessageBoxButton.OK); } x = meterStateFile; return(false); }
/// <summary> /// Save Current State on Exit /// </summary> /// <returns> true of successful</returns> public bool SaveCurrentPMState(object x) { MeterStateFile meterStateFile = (MeterStateFile)x; try { string meterStateFileJson = JsonConvert.SerializeObject(meterStateFile, Formatting.Indented); using (IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()) { if (!isolatedStorageFile.DirectoryExists("CarLocatorSystems")) { isolatedStorageFile.CreateDirectory("CarLocatorSystems"); }//end if IsolatedStorageFileStream fileStream = null; try { fileStream = isolatedStorageFile.OpenFile(@"CarLocatorSystems/MeterState.dat" , FileMode.Create); using (StreamWriter streamWriter = new StreamWriter(fileStream)) { fileStream = null; streamWriter.Write(meterStateFileJson); } //end using 3 } //end using 2 finally { if (fileStream != null) { fileStream.Dispose(); } } }//end using 1 return(true); } catch (Exception e) { MessageBox.Show(e.Message, "Error", MessageBoxButton.OK); } return(false); }