/// <summary> /// Split the current folder in an array of sub-folder names and return it. /// </summary> /// <returns>Returns a string array of su-folder names (including drive) or null if there are no sub-folders.</returns> public static string[] GetDirectories(string folder) { if (string.IsNullOrEmpty(folder) == true) { return(null); } folder = PathModel.NormalizePath(folder); string[] dirs = null; try { dirs = folder.Split(new char[] { System.IO.Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries); if (dirs != null) { if (dirs[0].Length == 2) // Normalizing Drive representation { // from 'C:' to 'C:\' if (dirs[0][1] == ':') // to ensure correct processing { dirs[0] += '\\'; // since 'C:' technically invalid(!) } } } } catch { } return(dirs); }
/// <summary> /// Returns a normalized directory reference from a path reference /// or the parent directory path if the <paramref name="dirPath"/> /// reference points to a file. /// </summary> /// <param name="dirPath"></param> /// <returns></returns> public static string ExtractDirectoryRoot(string dirPath) { bool bExists = false; if (PathModel.CheckValidString(dirPath) == false) { return(null); } try { bExists = System.IO.Directory.Exists(dirPath); } catch { } if (bExists == true) { return(PathModel.NormalizePath(dirPath)); } else { bExists = false; string path = string.Empty; try { // check if this is a file reference and attempt to get its path path = System.IO.Path.GetDirectoryName(dirPath); bExists = System.IO.Directory.Exists(path); } catch { } if (string.IsNullOrEmpty(path) == true) { return(null); } if (path.Length <= 3) { return(null); } if (bExists == true) { return(PathModel.NormalizePath(path)); } return(null); } }
/// <summary> /// Determine whether two <seealso cref="PathModel"/> objects describe the same /// location/item in the file system or not. /// /// Method implements <seealso cref="IComparable{IPathModel}"/> interface. /// </summary> /// <param name="path"></param> /// <returns></returns> public int CompareTo(IPathModel path) { if (PathModel.Compare(this, path) == true) { return(0); } else { string spath = PathModel.NormalizePath(path.Path); string thispath = PathModel.NormalizePath(this.Path); return(string.Compare(spath, thispath, true)); } }
/// <summary> /// Class constructor /// </summary> public PathModel(string path, FSItemType itemType) : this() { mItemType = itemType; switch (itemType) { case FSItemType.LogicalDrive: case FSItemType.File: mPath = PathModel.NormalizePath(path); break; case FSItemType.Folder: mPath = PathModel.NormalizePath(path); break; //// case FSItemType.DummyEntry: //// break; case FSItemType.Unknown: default: throw new NotImplementedException(string.Format("Enumeration member: '{0}' not supported.", itemType)); } }