/// <summary> /// Writes the genre template list. /// </summary> /// <returns> /// <c>true</c>, if genre template list was write, <c>false</c> otherwise. /// </returns> public static bool WriteGenreTemplateList() { try { MyMessages.NameOfMethod = MethodBase.GetCurrentMethod().Name; var directoryName = GenreFileItems.GetGenreUserTemplateListDirectory(); var templateListName = GenreFileItems.GetFileNameOfGenreTemplateList(); // var genreFilePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); var genreFilePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); genreFilePath = Path.Combine(genreFilePath, directoryName); genreFilePath = Path.Combine(genreFilePath, templateListName); var count = GenreDefaultListCollection.ItemCount(); using (var sw = new StreamWriter(genreFilePath)) { for (var i = 0; i < count; i++) { var genreName = GenreDefaultListCollection.GetItemAt(i); sw.WriteLine(genreName); } } return(true); } catch (FileNotFoundException ex) { MyMessages.ErrorMessage = "Unable to find file."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } catch (IOException ex) { MyMessages.ErrorMessage = "Encountered error while writing to file. Operation canceled."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } }
/// <summary> /// Display FileBrowser to select where to save backup copy of genre /// default list. /// </summary> /// <returns></returns> public static string SaveGenreDefaultListBackup() { string retVal; using (var sfd = new SaveFileDialog()) { sfd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; sfd.FilterIndex = 2; sfd.RestoreDirectory = true; if (sfd.ShowDialog() == DialogResult.OK) { using (var sw = new StreamWriter(sfd.FileName)) { var count = GenreDefaultListCollection.ItemCount(); for (var i = 0; i < count; i++) { // if first line of file write pound sign then add // first item in collection. if (i == 0) { sw.WriteLine("#"); } sw.WriteLine(GenreDefaultListCollection.GetItemAt(i)); } } retVal = sfd.FileName; } else { retVal = string.Empty; } } return(retVal); }
/// <summary> /// If the user has made a back up file then they can restore it from here. /// </summary> public static void RestoreGenreDefaultListFromBackup() { try { MyMessages.NameOfMethod = MethodBase.GetCurrentMethod().Name; var validate = false; GenreDefaultListCollection.ClearCollection(); using (var ofd = new OpenFileDialog()) { ofd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; ofd.FilterIndex = 2; ofd.RestoreDirectory = true; ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); if (ofd.ShowDialog() == DialogResult.OK) { using (var sr = new StreamReader(ofd.FileName)) { string genreName; while ((genreName = sr.ReadLine()) != null) { if (!validate) { validate = CheckForValidGenreName(genreName); } else { if (!string.IsNullOrEmpty(genreName)) { GenreDefaultListCollection.AddItem(genreName); } } } } } } if (GenreDefaultListCollection.ItemCount() <= 0) { return; } MyMessages.QuestionMessage = "This will overwrite your current genre default list." + " Do you wish to continue."; var result = MyMessages.ShowQuestionMessage(MyMessages.QuestionMessage, MyMessages.NameOfClass); if (result == DialogResult.Yes) { GenreFileReadWrite.WriteGenreTemplateList(); } } catch (FileNotFoundException ex) { MyMessages.ErrorMessage = "Not a valid path. Exiting operation."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); } catch (ArgumentNullException ex) { MyMessages.ErrorMessage = "Not a valid path. Exiting operation."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); } }