How does one extract each folder name from a path?

后端 未结 16 1541
傲寒
傲寒 2020-11-30 09:18

My path is \\\\server\\folderName1\\another name\\something\\another folder\\

How do I extract each folder name into a string if I don\'t know how many

相关标签:
16条回答
  • 2020-11-30 10:22

    I see your method Wolf5370 and raise you.

    internal static List<DirectoryInfo> Split(this DirectoryInfo path)
    {
        if(path == null) throw new ArgumentNullException("path");
        var ret = new List<DirectoryInfo>();
        if (path.Parent != null) ret.AddRange(Split(path.Parent));
        ret.Add(path);
        return ret;
    }
    

    On the path c:\folder1\folder2\folder3 this returns

    c:\

    c:\folder1

    c:\folder1\folder2

    c:\folder1\folder2\folder3

    In that order

    OR

    internal static List<string> Split(this DirectoryInfo path)
    {
        if(path == null) throw new ArgumentNullException("path");
        var ret = new List<string>();
        if (path.Parent != null) ret.AddRange(Split(path.Parent));
        ret.Add(path.Name);
        return ret;
    }
    

    will return

    c:\

    folder1

    folder2

    folder3

    0 讨论(0)
  • 2020-11-30 10:22
    public static IEnumerable<string> Split(this DirectoryInfo path)
    {
        if (path == null) 
            throw new ArgumentNullException("path");
        if (path.Parent != null)
            foreach(var d in Split(path.Parent))
                yield return d;
        yield return path.Name;
    }
    
    0 讨论(0)
  • 2020-11-30 10:22

    Inspired by the earlier answers, but simpler, and without recursion. Also, it does not care what the separation symbol is, as Dir.Parent covers this:

        /// <summary>
        /// Split a directory in its components.
        /// Input e.g: a/b/c/d.
        /// Output: d, c, b, a.
        /// </summary>
        /// <param name="Dir"></param>
        /// <returns></returns>
        public static IEnumerable<string> DirectorySplit(this DirectoryInfo Dir)
        {
            while (Dir != null)
            {
                yield return Dir.Name;
                Dir = Dir.Parent;
            }
        }
    

    Either stick this in a static class to create a nice extension method, or just leave out the this (and static).

    Usage example (as an extension method) to access the path parts by number:

        /// <summary>
        /// Return one part of the directory path.
        /// Path e.g.: a/b/c/d. PartNr=0 is a, Nr 2 = c.
        /// </summary>
        /// <param name="Dir"></param>
        /// <param name="PartNr"></param>
        /// <returns></returns>
        public static string DirectoryPart(this DirectoryInfo Dir, int PartNr)
        {
            string[] Parts = Dir.DirectorySplit().ToArray();
            int L = Parts.Length;
            return PartNr >= 0 && PartNr < L ? Parts[L - 1 - PartNr] : "";
        }
    

    Both above methods are now in my personal library, hence the xml comments. Usage example:

        DirectoryInfo DI_Data = new DirectoryInfo(@"D:\Hunter\Data\2019\w38\abc\000.d");
        label_Year.Text = DI_Data.DirectoryPart(3); // --> 2019
        label_Entry.Text = DI_Data.DirectoryPart(6);// --> 000.d
    
    0 讨论(0)
  • 2020-11-30 10:22

    I am adding to Matt Brunell's answer.

                string[] directories = myStringWithLotsOfFolders.Split(Path.DirectorySeparatorChar);
    
                string previousEntry = string.Empty;
                if (null != directories)
                {
                    foreach (string direc in directories)
                    {
                        string newEntry = previousEntry + Path.DirectorySeparatorChar + direc;
                        if (!string.IsNullOrEmpty(newEntry))
                        {
                            if (!newEntry.Equals(Convert.ToString(Path.DirectorySeparatorChar), StringComparison.OrdinalIgnoreCase))
                            {
                                Console.WriteLine(newEntry);
                                previousEntry = newEntry;
                            }
                        }
                    }
                }
    

    This should give you:

    "\server"

    "\server\folderName1"

    "\server\folderName1\another name"

    "\server\folderName1\another name\something"

    "\server\folderName1\another name\something\another folder\"

    (or sort your resulting collection by the string.Length of each value.

    0 讨论(0)
提交回复
热议问题