public void SerializeObject(string filename, ApplicationSetting objectToSerialize) { try { Stream stream = File.Open(filename, FileMode.Create); BinaryFormatter bFormatter = new BinaryFormatter(); bFormatter.Serialize(stream, objectToSerialize); stream.Close(); } catch (Exception e) { MessageBox.Show("Save application configuration is fail.\n" + e.Message); } }
/// <summary> /// Read application setting file /// </summary> /// <param name="filename">File name</param> /// <returns>ApplicationSetting object</returns> public ApplicationSetting DeSerializeObject(string filename) { ApplicationSetting objectToSerialize = new ApplicationSetting(false); try { Stream stream = File.Open(filename, FileMode.Open); BinaryFormatter bFormatter = new BinaryFormatter(); objectToSerialize = (ApplicationSetting)bFormatter.Deserialize(stream); stream.Close(); } catch (Exception e) { MessageBox.Show("Load current application configuration is fail.\n" + e.Message); } return(objectToSerialize); }
/// <summary> /// Load configuration setting values /// </summary> private void LoadAppConfiguration() { /* Check if setting file is exist */ if (File.Exists(AppConfig.CURRENT_SETTING)) { ApplicationSetting setTemp = DeSerializeObject(AppConfig.CURRENT_SETTING); if (setTemp != null) { if (File.GetLastWriteTime(AppConfig.CONFIG_RESOURCE).CompareTo(setTemp.SettingFileTime) == 0) { AppConfig = setTemp; } } } else { /* Setting file does not exist */ AppConfig = new ApplicationSetting(true); /* Load First item */ AppConfig.ListOfLearnItem = this.LoadData(); } if (AppConfig.ListOfLearnItem.Count >= 1) { if (AppConfig.RandomSort) { //++ Mod (20150821 NguyenPT) Shuffle random list items //AppConfig.ShuffleRandomListItem(); this.m_ListOfLearnItem = AppConfig.ShuffleRandomListItem(); //-- Mod (20150821 NguyenPT) } //++ Add (20150821 NguyenPT) Add data to list keep current learn item else { this.m_ListOfLearnItem = AppConfig.ListOfLearnItem; } //-- Add (20150821 NguyenPT) } }
private List<LearnItem> LoadDataFromFile(ApplicationSetting.ResourceLink fileResource) { List<LearnItem> listitem = new List<LearnItem>(); if (File.Exists(fileResource.path) == false) { MessageBox.Show("File source is not exists.\n" + fileResource.path); return listitem; } string[] contents = File.ReadAllLines(fileResource.path); listitem = ParseStringArray(contents, fileResource.Term1Lang, fileResource.Term2Lang); return listitem; }
/// <summary> /// Read application setting file /// </summary> /// <param name="filename">File name</param> /// <returns>ApplicationSetting object</returns> public ApplicationSetting DeSerializeObject(string filename) { ApplicationSetting objectToSerialize = new ApplicationSetting(false); try { Stream stream = File.Open(filename, FileMode.Open); BinaryFormatter bFormatter = new BinaryFormatter(); objectToSerialize = (ApplicationSetting)bFormatter.Deserialize(stream); stream.Close(); } catch (Exception e) { MessageBox.Show("Load current application configuration is fail.\n" + e.Message); } return objectToSerialize; }