How does one extract each folder name from a path?

后端 未结 16 1598
傲寒
傲寒 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:10

    Realise this is an old post, but I came across it looking - in the end I decided apon the below function as it sorted what I was doing at the time better than any of the above:

    private static List SplitDirectory(DirectoryInfo parent)
    {
        if (parent == null) return null;
        var rtn = new List();
        var di = parent;
    
        while (di.Name != di.Root.Name)
        {
        rtn.Add(new DirectoryInfo(di));
        di = di.Parent;
        }
        rtn.Add(new DirectoryInfo(di.Root));
    
        rtn.Reverse();
        return rtn;
    }
    

提交回复
热议问题