/// <summary> /// Checks the directory for files. /// </summary> /// <returns> /// <c>true</c>, if directory for files was checked, <c>false</c> otherwise. /// </returns> /// <param name="directoryPath">Directory path.</param> private bool CheckDirectoryForFiles(string directoryPath) { var retVal = false; try { MyMessages.NameOfMethod = MethodBase.GetCurrentMethod().Name; var getPaths = Directory.GetFiles(directoryPath); var pathLength = getPaths.Length; if (pathLength > 0) { retVal = FillCollectionWithSongs(getPaths); } // Return false no files found; return(retVal); } catch (PathTooLongException ex) { MyMessages.ErrorMessage = "This directory path is Invalid due to its length."; MyMessages.ErrorMessage = string.Concat(MyMessages.ErrorMessage, Environment.NewLine); MyMessages.ErrorMessage = string.Concat(MyMessages.ErrorMessage, directoryPath); MyMessages.BuildErrorString(MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(retVal); } catch (DirectoryNotFoundException ex) { MyMessages.ErrorMessage = "This directory path is incorrect. No directory was found."; MyMessages.ErrorMessage = string.Concat(MyMessages.ErrorMessage, Environment.NewLine); MyMessages.ErrorMessage = string.Concat(MyMessages.ErrorMessage, directoryPath); MyMessages.BuildErrorString(MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(retVal); } catch (UnauthorizedAccessException ex) { MyMessages.ErrorMessage = "You do not have authorization to access this directory."; MyMessages.ErrorMessage = string.Concat(MyMessages.ErrorMessage, Environment.NewLine); MyMessages.ErrorMessage = string.Concat(MyMessages.ErrorMessage, directoryPath); MyMessages.BuildErrorString(MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(retVal); } }
/// <summary> /// public static void ClearCollection() Clears the array. /// </summary> public static void ClearCollection() { try { MyMessages.NameOfClass = MethodBase.GetCurrentMethod().DeclaringType.Name; MyMessages.NameOfMethod = MethodBase.GetCurrentMethod().Name; fileDirName.Clear(); } catch (NullReferenceException ex) { MyMessages.ErrorMessage = "Encountered error while clearing the collection."; MyMessages.BuildErrorString(MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); } }
/// <summary> /// Gets the item at index. /// </summary> /// <returns>The <see cref="T:System.String" />.</returns> /// <param name="index">Index of item to get.</param> public static string GetItemAt(int index) { try { MyMessages.NameOfClass = MethodBase.GetCurrentMethod().DeclaringType.Name; MyMessages.NameOfMethod = MethodBase.GetCurrentMethod().Name; return(songPaths[index]); } catch (IndexOutOfRangeException ex) { MyMessages.ErrorMessage = "Encountered error while returning song path."; MyMessages.BuildErrorString(MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(null); } }
/// <summary> /// Validates the music directory. /// </summary> /// <returns> /// <c>true</c>, if music directory was validated, <c>false</c> otherwise. /// </returns> /// <param name="directoryPath">Directory path.</param> /// <exception cref="DirectoryNotFoundException"></exception> public static bool ValidateMusicDirectory(string directoryPath) { try { var declaringType = MethodBase.GetCurrentMethod().DeclaringType; if (declaringType != null) { MyMessages.NameOfClass = declaringType.Name; } MyMessages.NameOfMethod = MethodBase.GetCurrentMethod().Name; if (string.IsNullOrEmpty(directoryPath) || !Directory.Exists(directoryPath)) { throw new DirectoryNotFoundException(); } var songFiles = Directory.EnumerateFiles(directoryPath, "*.mp3", SearchOption.AllDirectories); var count = songFiles.Count(); return(count > 0); } catch (ArgumentNullException ex) { MyMessages.ErrorMessage = "Source file path is null."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } catch (ArgumentException ex) { MyMessages.ErrorMessage = "Source file path is not valid."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } catch (DirectoryNotFoundException ex) { MyMessages.ErrorMessage = "Unable to locate this directory: " + directoryPath; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } }
/// <summary> /// Gets all items. /// </summary> /// <returns>The all items.</returns> public static string[] GetAllItems() { string[] genreNames = null; try { var declaringType = MethodBase.GetCurrentMethod().DeclaringType; if (declaringType != null) { MyMessages.NameOfClass = declaringType.Name; } MyMessages.NameOfMethod = MethodBase.GetCurrentMethod().Name; var intCount = GenreList.Count; // No genre Folders Found if (intCount - 1 < 1) { return(new string[0]); } genreNames = new string[intCount]; for (var i = 0; i < intCount; i++) { genreNames[i] = GenreList[i]; } // All OK. return(genreNames); } catch (IndexOutOfRangeException ex) { MyMessages.ErrorMessage = "Encountered error while getting list from collection."; MyMessages.BuildErrorString(MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(genreNames); } catch (InvalidOperationException ex) { MyMessages.ErrorMessage = "Encountered error while getting list from collection."; MyMessages.BuildErrorString(MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(genreNames); } }
/// <summary> /// Make directory name upper case. /// </summary> /// <returns> /// <c>true</c>, if make upper case name was directory, <c>false</c> otherwise. /// </returns> /// <param name="dirName">Directory path.</param> /// <exception cref="DirectoryNotFoundException"></exception> /// <exception cref="ArgumentNullException"></exception> public string DirectoryMakeUpperCaseName(string dirName) { try { MyMessages.NameOfMethod = MethodBase.GetCurrentMethod().Name; dirName = dirName.Trim(); if (string.IsNullOrEmpty(dirName)) { throw new ArgumentNullException(); } var parentDirName = new DirectoryInfo(dirName).Parent.FullName; var origDirName = new DirectoryInfo(dirName).Name; var newDirName = origDirName.ToUpperInvariant(); var comp = string.Compare(origDirName, newDirName, StringComparison.CurrentCultureIgnoreCase); return(newDirName); } catch (ArgumentNullException ex) { MyMessages.ErrorMessage = "The directory path is not valid."; MyMessages.BuildErrorString(MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(string.Empty); } catch (ArgumentException ex) { MyMessages.ErrorMessage = "The directory path is not valid."; MyMessages.BuildErrorString(MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(string.Empty); } catch (DirectoryNotFoundException ex) { MyMessages.ErrorMessage = "The directory path is not valid."; MyMessages.BuildErrorString(MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(string.Empty); } }
/// <summary> /// Sorts the collection. /// </summary> /// <returns><c>true</c>, if collection was sorted, <c>false</c> otherwise.</returns> public static bool SortCollection() { var bolRetVal = false; try { GenreList.Sort(); return(bolRetVal = true); } catch (InvalidOperationException ex) { MyMessages.ErrorMessage = "Unable to sort collection."; MyMessages.NameOfClass = MethodBase.GetCurrentMethod().DeclaringType.Name; MyMessages.NameOfMethod = MethodBase.GetCurrentMethod().Name; MyMessages.BuildErrorString(MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(bolRetVal); } }
/// <summary> /// Gets all items. /// </summary> /// <returns>The all items.</returns> public static string[] GetAllItems() { string[] origPath = null; try { MyMessages.NameOfClass = MethodBase.GetCurrentMethod().DeclaringType.Name; MyMessages.NameOfMethod = MethodBase.GetCurrentMethod().Name; var cnt = fileDirName.Count; // No genre Folders Found if (cnt - 1 < 1) { return(origPath); } origPath = new string[cnt]; for (var i = 0; i < cnt; i++) { origPath[i] = fileDirName[i]; } // All OK. return(origPath); } catch (IndexOutOfRangeException ex) { MyMessages.ErrorMessage = "Encountered error while getting list from collection."; MyMessages.BuildErrorString(MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(origPath); } catch (InvalidOperationException ex) { MyMessages.ErrorMessage = "Encountered error while getting list from collection."; MyMessages.BuildErrorString(MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(origPath); } }
/// <summary> /// Adds the item. /// </summary> /// <returns><c>true</c>, if item was added, <c>false</c> otherwise.</returns> /// <param name="genreName">Genre name.</param> public static bool AddItem(string genreName) { try { var declaringType = MethodBase.GetCurrentMethod().DeclaringType; if (declaringType != null) { MyMessages.NameOfClass = declaringType.Name; } MyMessages.NameOfMethod = MethodBase.GetCurrentMethod().Name; if (!ContainsItem(genreName)) { GenreList.Add(genreName); } else { return(false); } return(true); } catch (ArgumentNullException ex) { MyMessages.ErrorMessage = "Encountered error while adding new song path to this collection: "; MyMessages.ErrorMessage = string.Concat(MyMessages.ErrorMessage, Environment.NewLine); MyMessages.ErrorMessage = string.Concat(MyMessages.ErrorMessage, genreName); MyMessages.BuildErrorString(MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } catch (NullReferenceException ex) { MyMessages.ErrorMessage = "Encountered error while adding new song path to this collection: "; MyMessages.ErrorMessage = string.Concat(MyMessages.ErrorMessage, Environment.NewLine); MyMessages.ErrorMessage = string.Concat(MyMessages.ErrorMessage, genreName); MyMessages.BuildErrorString(MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } }
/// <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> /// Itemses the count. /// </summary> /// <returns>The count.</returns> public static int ItemCount() { var intCnt = 0; try { MyMessages.NameOfClass = MethodBase.GetCurrentMethod().DeclaringType.Name; MyMessages.NameOfMethod = MethodBase.GetCurrentMethod().Name; intCnt = songPaths.Count; // All OK return(intCnt); } catch (NullReferenceException ex) { MyMessages.ErrorMessage = "Encountered error while getting count of items contained in the collection."; MyMessages.BuildErrorString(MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(intCnt); } }
/// <summary> /// Reads the genre template list. Used to read in the list of genre /// directories the user has and to create, change and add to the genre /// directories list. The list is found at: /home/user-name/.local/share/MusicManager/Genre-Template-List /// </summary> /// <returns> /// <c>true</c>, if genre template list was read, <c>false</c> otherwise. /// </returns> public static bool ReadGenreTemplateList() { try { MyMessages.NameOfMethod = MethodBase.GetCurrentMethod().Name; var directoryName = GenreFileItems.GetGenreUserTemplateListDirectory(); var templateListName = GenreFileItems.GetFileNameOfGenreTemplateList(); var genreFilePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); genreFilePath = Path.Combine(genreFilePath, directoryName); genreFilePath = Path.Combine(genreFilePath, templateListName); // Read the file and display it line by line. using (var genreSr = new StreamReader(genreFilePath)) { string genreName; while ((genreName = genreSr.ReadLine()) != null) { if (!string.IsNullOrEmpty(genreName)) { GenreDefaultListCollection.AddItem(genreName); } } GenreDefaultListCollection.SortCollection(); // All OK return(true); } } catch (FileNotFoundException ex) { MyMessages.ErrorMessage = "Unable to locate this file. Possibly it has not been created yet."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } }
/// <summary> /// Gets the index of the item. /// </summary> /// <returns>The item index.</returns> /// <param name="genreName">Genre name.</param> public static int GetItemIndex(string genreName) { var intIndex = -1; try { var declaringType = MethodBase.GetCurrentMethod().DeclaringType; if (declaringType != null) { MyMessages.NameOfClass = declaringType.Name; } MyMessages.NameOfMethod = MethodBase.GetCurrentMethod().Name; intIndex = GenreList.IndexOf(genreName); // All OK return(intIndex); } catch (ArgumentException ex) { MyMessages.ErrorMessage = "Encountered error getting Index of this item."; MyMessages.ErrorMessage = string.Concat(MyMessages.ErrorMessage, Environment.NewLine); MyMessages.ErrorMessage = string.Concat(MyMessages.ErrorMessage, genreName); MyMessages.BuildErrorString(MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(intIndex); } catch (NullReferenceException ex) { MyMessages.ErrorMessage = "Encountered error getting Index of this item."; MyMessages.ErrorMessage = string.Concat(MyMessages.ErrorMessage, Environment.NewLine); MyMessages.ErrorMessage = string.Concat(MyMessages.ErrorMessage, genreName); MyMessages.BuildErrorString(MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(intIndex); } }
/// <summary> /// Adds the item. /// </summary> /// <param name="originalPath">Original path.</param> public static void AddItem(string originalPath) { try { MyMessages.NameOfClass = MethodBase.GetCurrentMethod().DeclaringType.Name; MyMessages.NameOfMethod = MethodBase.GetCurrentMethod().Name; fileDirName.Add(originalPath); } catch (ArgumentNullException ex) { MyMessages.ErrorMessage = "Encountered error while adding path to this collection: " + originalPath; MyMessages.BuildErrorString(MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); } catch (NullReferenceException ex) { MyMessages.ErrorMessage = "Encountered error while adding path to this collection: " + originalPath; MyMessages.BuildErrorString(MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); } }
/// <summary> /// Removes the item at index. /// </summary> /// <returns>The <see cref="System.Boolean" />.</returns> /// <param name="index">Index-of the item to remove.</param> public static bool RemoveItemAt(int index) { var bolRetVal = false; try { if (index < 0) { return(bolRetVal = true); } GenreList.RemoveAt(index); return(bolRetVal = true); } catch (IndexOutOfRangeException ex) { MyMessages.ErrorMessage = "Encountered error removing item at Index: "; MyMessages.ErrorMessage = string.Concat(MyMessages.ErrorMessage, Environment.NewLine); MyMessages.ErrorMessage = string.Concat(MyMessages.ErrorMessage, index.ToString()); MyMessages.NameOfClass = MethodBase.GetCurrentMethod().DeclaringType.Name; MyMessages.NameOfMethod = MethodBase.GetCurrentMethod().Name; MyMessages.BuildErrorString(MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(bolRetVal); } catch (NullReferenceException ex) { MyMessages.ErrorMessage = "Encountered error removing item at Index: "; MyMessages.ErrorMessage = string.Concat(MyMessages.ErrorMessage, Environment.NewLine); MyMessages.ErrorMessage = string.Concat(MyMessages.ErrorMessage, index.ToString()); MyMessages.NameOfClass = MethodBase.GetCurrentMethod().DeclaringType.Name; MyMessages.NameOfMethod = MethodBase.GetCurrentMethod().Name; MyMessages.BuildErrorString(MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(bolRetVal); } }
/// <summary> /// Removes the item. /// </summary> /// <returns><c>true</c>, if item was removed, <c>false</c> otherwise.</returns> /// <param name="genreName">Genre name.</param> public static bool RemoveItem(string genreName) { var bolRetVal = false; try { var declaringType = MethodBase.GetCurrentMethod().DeclaringType; if (declaringType != null) { MyMessages.NameOfClass = declaringType.Name; } MyMessages.NameOfMethod = MethodBase.GetCurrentMethod().Name; GenreList.Remove(genreName); return(bolRetVal = true); } catch (ArgumentException ex) { MyMessages.ErrorMessage = "Encountered error while removing this item."; MyMessages.ErrorMessage = string.Concat(MyMessages.ErrorMessage, Environment.NewLine); MyMessages.ErrorMessage = string.Concat(MyMessages.ErrorMessage, genreName); MyMessages.BuildErrorString(MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(bolRetVal); } catch (NullReferenceException ex) { MyMessages.ErrorMessage = "Encountered error while removing this item."; MyMessages.ErrorMessage = string.Concat(MyMessages.ErrorMessage, Environment.NewLine); MyMessages.ErrorMessage = string.Concat(MyMessages.ErrorMessage, genreName); MyMessages.BuildErrorString(MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(bolRetVal); } }
/// <summary> /// Contains the item. /// </summary> /// <returns><c>true</c>, if item was contained, <c>false</c> otherwise.</returns> /// <param name="genreName">Genre name.</param> private static bool ContainsItem(string genreName) { try { var declaringType = MethodBase.GetCurrentMethod().DeclaringType; if (declaringType != null) { MyMessages.NameOfClass = declaringType.Name; } MyMessages.NameOfMethod = MethodBase.GetCurrentMethod().Name; return(GenreList.Contains(genreName)); } catch (ArgumentException ex) { MyMessages.ErrorMessage = "Genre Directory Name is invalid and will not be added to the list."; MyMessages.ErrorMessage = string.Concat(MyMessages.ErrorMessage, Environment.NewLine); MyMessages.ErrorMessage = string.Concat(MyMessages.ErrorMessage, genreName); MyMessages.BuildErrorString(MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } }
/// <summary> /// Inserts the item at position and item. /// </summary> /// <returns>The <see cref="System.Boolean" />.</returns> /// <param name="pos">Position.</param> /// <param name="item">Item to be inserted.</param> public static bool InsertItemAt(int pos, string item) { var bolRetVal = false; try { var declaringType = MethodBase.GetCurrentMethod().DeclaringType; if (declaringType != null) { MyMessages.NameOfClass = declaringType.Name; } MyMessages.NameOfMethod = MethodBase.GetCurrentMethod().Name; GenreList.Insert(pos, item); return(bolRetVal = true); } catch (ArgumentNullException ex) { MyMessages.ErrorMessage = "Encountered error while adding new genre directory to this collection."; MyMessages.ErrorMessage = string.Concat(MyMessages.ErrorMessage, Environment.NewLine); MyMessages.ErrorMessage = string.Concat(MyMessages.ErrorMessage, item); MyMessages.BuildErrorString(MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(bolRetVal); } catch (NullReferenceException ex) { MyMessages.ErrorMessage = "Encountered error while adding new genre directory to this collection."; MyMessages.ErrorMessage = string.Concat(MyMessages.ErrorMessage, Environment.NewLine); MyMessages.ErrorMessage = string.Concat(MyMessages.ErrorMessage, item); MyMessages.BuildErrorString(MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(bolRetVal); } }
/// <summary> /// Display file browser dialog so user can Selects the top-level music /// directory. Then verify that the user has selected a directory /// containing .mp3 files. /// </summary> /// <returns>Path to the users music directory</returns> public static string SelectToplevelMusicDirectory() { try { MyMessages.NameOfMethod = MethodBase.GetCurrentMethod().Name; using (var fdb = new FolderBrowserDialog()) { // Set the help text description for the FolderBrowserDialog. fdb.Description = "Select your top level music directory."; // Do not allow the user to create new files via the FolderBrowserDialog. fdb.ShowNewFolderButton = false; // Default to the My Documents folder. fdb.RootFolder = Environment.SpecialFolder.MyComputer; var retVal = fdb.ShowDialog(); if (retVal != DialogResult.OK) { return(string.Empty); } UserEnviormentInfoProperties.UserMusicDirectoryPath = fdb.SelectedPath; return(fdb.SelectedPath); } } catch (IOException ex) { MyMessages.ErrorMessage = "Encountered error while selecting Music directory. "; MyMessages.NameOfMethod = MethodBase.GetCurrentMethod().Name; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(string.Empty); } }
/// <summary> /// Adds the Genre directory names to the collection. /// </summary> /// <returns><c>true</c>, if item was added, <c>false</c> otherwise.</returns> /// <param name="genreName">Genre name.</param> public static bool AddItem(string genreName) { var bolRetVal = false; try { if (!ContainsItem(genreName)) { GenreList.Add(genreName); } return(bolRetVal = true); } catch (ArgumentException ex) { MyMessages.ErrorMessage = "Genre Directory Name is invalid and will not be added to the list."; MyMessages.ErrorMessage = string.Concat(MyMessages.ErrorMessage, Environment.NewLine); MyMessages.ErrorMessage = string.Concat(MyMessages.ErrorMessage, genreName); MyMessages.NameOfClass = MethodBase.GetCurrentMethod().DeclaringType.Name; MyMessages.NameOfMethod = MethodBase.GetCurrentMethod().Name; MyMessages.BuildErrorString(MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(bolRetVal); } catch (NullReferenceException ex) { MyMessages.ErrorMessage = "Encountered error while adding new genre directory to this collection."; MyMessages.ErrorMessage = string.Concat(MyMessages.ErrorMessage, Environment.NewLine); MyMessages.ErrorMessage = string.Concat(MyMessages.ErrorMessage, genreName); MyMessages.NameOfClass = MethodBase.GetCurrentMethod().DeclaringType.Name; MyMessages.NameOfMethod = MethodBase.GetCurrentMethod().Name; MyMessages.BuildErrorString(MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(bolRetVal); } }
/// <summary> /// Create Make Proper name from current directory name. /// </summary> /// <returns> /// <c>true</c>, if make proper case name was directory, <c>false</c> otherwise. /// </returns> /// <param name="dirName">Directory path.</param> /// <exception cref="DirectoryNotFoundException"></exception> /// <exception cref="ArgumentNullException"></exception> public string DirectoryMakeProperCaseName(string dirName) { try { MyMessages.NameOfMethod = MethodBase.GetCurrentMethod().Name; dirName = dirName.Trim(); if (string.IsNullOrEmpty(dirName)) { throw new ArgumentNullException(); } string newDirPath; var parentDirName = new DirectoryInfo(dirName).Parent.FullName; var origDirName = new DirectoryInfo(dirName).Name; var newDirName = string.Empty; var myTI = new CultureInfo("en-US", false).TextInfo; var newLowerCase = myTI.ToLower(origDirName); newDirName = myTI.ToTitleCase(newLowerCase); var comp = string.Compare(origDirName, newDirName, StringComparison.CurrentCultureIgnoreCase); if (comp == 0) { OriginalDirectoryFilePathsCollection.AddItem(dirName); OriginalDirectoryFileNamesCollection.AddItem(origDirName); newDirPath = Path.Combine(parentDirName, newDirName); SongRecordProperties.NewDirectoryPath = newDirPath; NewDirectoryFilePathsCollection.AddItem(newDirPath); NewDirectoryFileNameCollection.AddItem(newDirName); } else { newDirPath = string.Empty; } return(newDirPath); } catch (ArgumentNullException ex) { MyMessages.ErrorMessage = "The directory path is not valid."; MyMessages.BuildErrorString(MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(string.Empty); } catch (ArgumentException ex) { MyMessages.ErrorMessage = "The directory path is not valid."; MyMessages.BuildErrorString(MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(string.Empty); } catch (DirectoryNotFoundException ex) { MyMessages.ErrorMessage = "The directory path is not valid."; MyMessages.BuildErrorString(MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(string.Empty); } }
/// <summary> /// Deletes the directory. /// </summary> /// <param name="dirPath">Dir path.</param> /// <exception cref="DirectoryNotFoundException"> /// Unable to locate this directory. /// </exception> public static bool DeleteDirectory(string dirPath) { try { MyMessages.NameOfMethod = MethodBase.GetCurrentMethod().Name; if (!Directory.Exists(dirPath)) { throw new DirectoryNotFoundException(); } const bool deleteFilesRecursively = true; Directory.Delete(dirPath, deleteFilesRecursively); // All OK return(true); } catch (UnauthorizedAccessException ex) { MyMessages.ErrorMessage = "You do not have the necessary permission level for this operation."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } catch (ArgumentNullException ex) { MyMessages.ErrorMessage = "The directory path is missing."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } catch (ArgumentException ex) { MyMessages.ErrorMessage = "The directory path is invalid."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } catch (PathTooLongException ex) { MyMessages.ErrorMessage = "The directory path is to long."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } catch (DirectoryNotFoundException ex) { MyMessages.ErrorMessage = "Found no directory at this path: " + dirPath; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } catch (IOException ex) { MyMessages.ErrorMessage = "Encountered error while deleting the directory. Aborting operation."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } catch (NotSupportedException ex) { MyMessages.ErrorMessage = "This delete directory encountered an error. Operation aborted."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } }
/// <summary> /// Deletes the file. /// </summary> /// <returns><c>true</c>, if file was deleted, <c>false</c> otherwise.</returns> /// <param name="filePath">File path.</param> /// <exception cref="ArgumentNullException"> /// The directory path is null. /// </exception> public static bool DeleteFile(string filePath) { try { MyMessages.NameOfMethod = MethodBase.GetCurrentMethod().Name; if (string.IsNullOrEmpty(filePath)) { throw new ArgumentNullException(); } if (File.Exists(filePath)) { MyMessages.QuestionMessage = "A file with this name already exists: " + " Are you sure you wish to delete it. " + filePath; var result = MyMessages.ShowQuestionMessageBox(); if (result != DialogResult.Yes) { return(false); } } if (File.Exists(filePath)) { MyMessages.QuestionMessage = "A file with this name already exists: " + " Are you sure you wish to delete it. " + filePath; var result = MyMessages.ShowQuestionMessageBox(); if (result != DialogResult.Yes) { return(false); } } File.Delete(filePath); // All OK MyMessages.InformationMessage = "This file has been deleted successfully"; MyMessages.ShowInformationMessageBox(); // All OK return(true); } catch (UnauthorizedAccessException ex) { MyMessages.ErrorMessage = "You do not have the necessary permission level for this operation."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } catch (ArgumentNullException ex) { MyMessages.ErrorMessage = "Either the file source or the destination file path is null."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } catch (ArgumentException ex) { MyMessages.ErrorMessage = "Either the file source or the destination file path is invalid."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } catch (PathTooLongException ex) { MyMessages.ErrorMessage = "Either the file source or destination file path is to long."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } catch (FileNotFoundException ex) { MyMessages.ErrorMessage = "The source file was not found at this path. " + filePath; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } catch (IOException ex) { MyMessages.ErrorMessage = "Encountered error while copying source file to destination."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } catch (NotSupportedException ex) { MyMessages.ErrorMessage = "This file copy encountered an error. Operation aborted."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } }
/// <summary> /// Validates the destination file exists. /// </summary> /// <returns> /// <c>true</c>, if destination file exists was validated, <c>false</c> otherwise. /// </returns> /// <param name="filePath">File path.</param> /// <exception cref="ArgumentNullException"></exception> public static bool ValidateDestinationFileExists(string filePath) { var sb = new StringBuilder(); try { var declaringType = MethodBase.GetCurrentMethod().DeclaringType; if (declaringType != null) { MyMessages.NameOfClass = declaringType.Name; } MyMessages.NameOfMethod = MethodBase.GetCurrentMethod().Name; if (string.IsNullOrEmpty(filePath)) { throw new ArgumentNullException(); } if (File.Exists(filePath)) { sb.Append("A file with the destination name all ready exists."); sb.Append(filePath); sb.Append("Do you wish to delete this existing file."); MyMessages.QuestionMessage = sb.ToString(); var rspAns = MyMessages.ShowQuestionMessageBox(); if (rspAns == DialogResult.No) { MyMessages.InformationMessage = "Canceling this operation. The file will not be saved."; MyMessages.ShowInformationMessageBox(); return(false); } File.Delete(filePath); } return(true); } catch (ArgumentNullException ex) { MyMessages.ErrorMessage = "Source file path is null."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } catch (ArgumentException ex) { MyMessages.ErrorMessage = "Source file path is not valid."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } catch (FileNotFoundException ex) { MyMessages.ErrorMessage = "Unable to locate this file."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } }
/// <summary> /// Moves the directory. /// </summary> /// <returns><c>true</c>, if directory was moved, <c>false</c> otherwise.</returns> /// <param name="sourceDir">Source directory to move.</param> /// <param name="destDir">Destination directory.</param> /// <exception cref="ArgumentNullException">The file path is null.</exception> /// <exception cref="DirectoryNotFoundException"> /// The file path is invalid. /// </exception> /// <exception cref="IOException"> /// Source or destination directory path null. /// </exception> public static bool MoveDirectory(string sourceDir, string destDir) { try { MyMessages.NameOfMethod = MethodBase.GetCurrentMethod().Name; if (string.IsNullOrEmpty(sourceDir)) { throw new ArgumentNullException(); } if (!Directory.Exists(sourceDir)) { throw new DirectoryNotFoundException(); } if (string.IsNullOrEmpty(destDir)) { throw new ArgumentNullException(); } if (Directory.Exists(destDir)) { MyMessages.QuestionMessage = "A destination directory with this name all ready exists: " + destDir + " Do you wish to delete it?"; var result = MyMessages.ShowQuestionMessageBox(); if (result == DialogResult.Yes) { if (!DeleteDirectory(destDir)) { throw new IOException(); } } } Directory.Move(sourceDir, destDir); return(!Directory.Exists(destDir)); } catch (UnauthorizedAccessException ex) { MyMessages.ErrorMessage = "You do not have the necessary permission level for this operation."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } catch (ArgumentNullException ex) { MyMessages.ErrorMessage = "The source directory path or destination directory path is missing."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } catch (ArgumentException ex) { MyMessages.ErrorMessage = "The source directory path or destination directory path is invalid."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } catch (PathTooLongException ex) { MyMessages.ErrorMessage = "The source directory path or destination directory path is to long."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } catch (DirectoryNotFoundException ex) { MyMessages.ErrorMessage = "Found no directory at this path: " + sourceDir; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } catch (IOException ex) { MyMessages.ErrorMessage = "Encountered error while deleting the directory. Aborting operation."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } catch (NotSupportedException ex) { MyMessages.ErrorMessage = "Encountered an error. Operation aborted."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } }
/// <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); } }
/// <summary> /// Moves the file. /// </summary> /// <returns><c>true</c>, if file was moved, <c>false</c> otherwise.</returns> /// <param name="sourceFile">Source file.</param> /// <param name="destFile">Destination file.</param> /// <exception cref="FileNotFoundException"> /// Unable to locate this source file or destination file. /// </exception> /// <exception cref="ArgumentException"></exception> /// <exception cref="ArgumentNullException"> /// Either the source file path or the destination file path is null. /// </exception> public static bool MoveFile(string sourceFile, string destFile) { try { MyMessages.NameOfMethod = MethodBase.GetCurrentMethod().Name; if (string.IsNullOrEmpty(sourceFile)) { throw new ArgumentNullException(); } if (!File.Exists(sourceFile)) { throw new FileNotFoundException(); } if (string.IsNullOrEmpty(destFile) || File.Exists(destFile)) { throw new ArgumentException(); } if (File.Exists(destFile)) { MyMessages.QuestionMessage = "A file with this name already exists: " + " Are you sure you wish to overwrite it. " + destFile; var result = MyMessages.ShowQuestionMessageBox(); if (result != DialogResult.Yes) { return(false); } } File.Move(sourceFile, destFile); return(true); } catch (UnauthorizedAccessException ex) { MyMessages.ErrorMessage = "You do not have the necessary permission level for this operation."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } catch (ArgumentNullException ex) { MyMessages.ErrorMessage = "The source file path or destination file path is missing."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } catch (ArgumentException ex) { MyMessages.ErrorMessage = "The source file path or destination file path is not valid."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } catch (PathTooLongException ex) { MyMessages.ErrorMessage = "Either the file source or destination file path is to long."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } catch (FileNotFoundException ex) { MyMessages.ErrorMessage = "The source file path or destination file path is not valid."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } catch (IOException ex) { MyMessages.ErrorMessage = "The file path is not correctly formed. Aborting. operation."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } catch (NotSupportedException ex) { MyMessages.ErrorMessage = "This file move encountered an error. Operation aborted."; MyMessages.BuildErrorString( MyMessages.NameOfClass, MyMessages.NameOfMethod, MyMessages.ErrorMessage, ex.Message); return(false); } }