Example #1
0
        /// <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);
            }
        }
Example #2
0
        /// <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);
        }