/// <summary> /// Save data to file /// </summary> /// <param name="attempts">Number of attempts</param> /// <returns>True, if save was succesful</returns> public async Task <bool> SaveDataAsync(int attempts = 0) { try { var serializer = new XmlSerializer(typeof(UpdateHistoryFile)); var version = Package.Current.Id.Version; Version packageVersion = new Version(version.Major, version.Minor, version.Build, version.Revision); using (Stream xmlStream = await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync(FileName, CreationCollisionOption.ReplaceExisting)) { serializer.Serialize(xmlStream, new UpdateHistoryFile() { UpdateList = this.UpdateList, LastVersion = packageVersion.ToString() }); } return(true); } // When file is unavailable catch (Exception e) when(e.Message.Contains("is in use") && (attempts < 10)) { await LogService.AddLogMessageAsync("File is in use\n\n" + e.Message); return(await SaveDataAsync(attempts + 1)); } catch (Exception e) { throw new Exceptions.Exception("Unhandled exception", e); } }
/// <summary> /// Save data to file /// </summary> /// <param name="data">Data to save</param> /// <param name="fileName">File name</param> /// <param name="path">Path to file</param> /// <param name="attempts">Number of attempts</param> /// <returns>True, if save was succesful</returns> public static async Task <bool> SaveDataAsync(T data, string fileName, string path, int attempts = 0) { Stream xmlStream = null; try { var serializer = new XmlSerializer(typeof(T)); xmlStream = await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync(fileName, CreationCollisionOption.ReplaceExisting); using (xmlStream) { serializer.Serialize(xmlStream, data); } return(true); } // When file is unavailable catch (Exception s) when((s.Message.Contains("denied") || s.Message.Contains("is in use")) && (attempts <= 10)) { return(await SaveDataAsync(data, fileName, path, attempts + 1)); } catch (Exception e) { await LogService.AddLogMessageAsync(e.Message); } finally { xmlStream?.Close(); } return(false); }
/// <summary> /// Read data from file /// </summary> /// <param name="attempts">Number of attempts</param> /// <returns>Collection of items of type T</returns> public async Task ReadDataAsync(int attempts = 0) { try { XmlSerializer Serializ = new XmlSerializer(typeof(UpdateHistoryFile)); object readedObjects = null; if ((await ApplicationData.Current.LocalFolder.GetFilesAsync()).FirstOrDefault(x => x.Name == FileName) != null) { using (var xmlStream = await ApplicationData.Current.LocalFolder.OpenStreamForReadAsync(FileName)) readedObjects = (UpdateHistoryFile)Serializ.Deserialize(xmlStream); } if (readedObjects != null) { LastVersion = new Version(((UpdateHistoryFile)readedObjects).LastVersion); UpdateList = ((UpdateHistoryFile)readedObjects).UpdateList; } else { LastVersion = new Version(0, 0, 0, 0); UpdateList = new List <UpdateItem>(); } } // When is file unavailable - 10 attempts is enough catch (Exception e) when(e.Message.Contains("is in use") && attempts < 10) { await LogService.AddLogMessageAsync("File is in use\n\n" + e.Message); await ReadDataAsync(attempts + 1); } catch (Exception e) { throw new Exceptions.Exception("Unhandled exception", e); } finally { LastVersion = new Version(0, 0, 0, 0); UpdateList = new List <UpdateItem>(); } }
/// <summary> /// Read data from file /// </summary> /// <param name="fileName">File name</param> /// <param name="path">Path to file</param> /// <param name="attempts">Number of attempts</param> /// <returns>Collection of items of type T</returns> public static async Task <T> ReadDataAsync(string fileName, string path, int attempts = 0) { T readedObjects = default(T); Stream xmlStream = null; try { XmlSerializer serializ = new XmlSerializer(typeof(T)); xmlStream = await ApplicationData.Current.LocalFolder.OpenStreamForReadAsync(fileName); using (xmlStream) { readedObjects = (T)serializ.Deserialize(xmlStream); } if (readedObjects != null) { return(readedObjects); } else { return(default(T)); } } // When is file unavailable - 10 attempts is enough catch (Exception s) when((s.Message.Contains("denied")) && (attempts < 10)) { return(await ReadDataAsync(fileName, path, attempts + 1)); } catch (Exception e) { await LogService.AddLogMessageAsync(e.Message); } finally { xmlStream?.Close(); } return(default(T)); }
// Insp protected async Task GenerateTimelineActivityAsync() { try { //Get the default UserActivityChannel and query it for our UserActivity. If the activity doesn't exist, one is created. UserActivityChannel channel = UserActivityChannel.GetDefault(); UserActivity userActivity = await channel.GetOrCreateUserActivityAsync("MainPage"); //Populate required properties userActivity.VisualElements.DisplayText = "Hello Activities"; userActivity.ActivationUri = new Uri("thedailynotes://page2?action=edit"); //Save await userActivity.SaveAsync(); //save the new metadata //Dispose of any current UserActivitySession, and create a new one. currentActivity?.Dispose(); currentActivity = userActivity.CreateSession(); } catch (Exception e) { await LogService.AddLogMessageAsync(e.Message); } }