using System; using System.Collections.Generic; using System.IO; using System.Text.RegularExpressions; using Sarand.Net.Standard.Base.App; using Sarand.Net.Standard.Base.Core; namespace Sarand.Net.Standard.Base.IO { //======================================================================= public static class DirectoryUtil { //------------------------------------------------------------------- // D i r e c t o r y E x i s t s //------------------------------------------------------------------- public static bool DirectoryExists (String directory) { try { return Directory.Exists(directory); } catch (Exception) { } return false; } //------------------------------------------------------------------- // G e t D i r e c t o r y N a m e //------------------------------------------------------------------- /// /// Returns the directory information for the specified path string. /// /// The path of a file or directory. /// Directory information for path, or null if path denotes a root directory or is /// null. Returns System.String.Empty if path does not contain directory information. public static String GetDirectoryName (String path) { try { // Exceptions: // T:System.ArgumentException: // The path parameter contains invalid characters, is empty, or contains only white spaces. // T:System.IO.PathTooLongException: // The path parameter is longer than the system-defined maximum length. return Path.GetDirectoryName(path); } catch (Exception) { } return String.Empty; } //------------------------------------------------------------------- // E n u m e r a t e D i r e c t o r i e s //------------------------------------------------------------------- /// /// Retrieve list of sub directories in directory. /// Avoiding Exception thrown when network path isn't valid. /// public static IEnumerable EnumerateDirectories (String path) { try { return Directory.EnumerateDirectories(path); } catch (Exception exc) { SaLogger.Error("DirectoryUtil.EnumerateDirectories " + exc.GetType().Name + ": " + exc.Message + ". Path: '" + path + "'"); } return new List(); } //------------------------------------------------------------------- // E n u m e r a t e F i l e s //------------------------------------------------------------------- /// /// Retrieve list of files in directory. /// Avoiding Exception thrown when network path isn't valid. /// public static IEnumerable EnumerateFiles (String path) { try { return Directory.EnumerateFiles(path); } catch (Exception exc) { SaLogger.Error("DirectoryUtil.EnumerateFiles " + exc.GetType().Name + ": " + exc.Message + ". Path: '" + path + "'"); } return new List(); } //------------------------------------------------------------------- /// /// Retrieve list of files in directory. /// Avoiding Exception thrown when network path isn't valid. /// public static IEnumerable EnumerateFiles (String path, String searchPattern) { try { return Directory.EnumerateFiles(path, searchPattern); } catch (Exception exc) { SaLogger.Error("DirectoryUtil.EnumerateFiles " + exc.GetType().Name + ": " + exc.Message + ". Path: '" + path + "'. Pattern: " + searchPattern); } return new List(); } //------------------------------------------------------------------- /// /// Retrieve list of files in directory. /// Avoiding Exception thrown when network path isn't valid. /// public static IEnumerable EnumerateFiles (String path, String searchPattern, SearchOption searchOption) { try { return Directory.EnumerateFiles(path, searchPattern, searchOption); } catch (Exception exc) { SaLogger.Error("DirectoryUtil.EnumerateFiles " + exc.GetType().Name + ": " + exc.Message + ". Path: '" + path + "'. Pattern: " + searchPattern + ". Option: " + searchOption.ToString()); } return new List(); } //------------------------------------------------------------------- // C r e a t e D i r e c t o r y //------------------------------------------------------------------- /// /// Create Directory in the specified path. /// Avoiding Exception thrown when network path isn't valid. /// public static DirectoryInfo CreateDirectory (String path) { try { return Directory.CreateDirectory(path); } catch (Exception exc) { SaLogger.Error("DirectoryUtil.CreateDirectory " + exc.GetType().Name + ": " + exc.Message + ". Path: '" + path + "'"); } return null; } //------------------------------------------------------------------- // Utilities. public static String TrimTrailingSlashes (String path) { return Regex.Replace(path, "[\\\\/]+$", ""); } //------------------------------------------------------------------- } //======================================================================= }