How does one extract each folder name from a path?

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

    There are a few ways that a file path can be represented. You should use the System.IO.Path class to get the separators for the OS, since it can vary between UNIX and Windows. Also, most (or all if I'm not mistaken) .NET libraries accept either a '\' or a '/' as a path separator, regardless of OS. For this reason, I'd use the Path class to split your paths. Try something like the following:

    string originalPath = "\\server\\folderName1\\another\ name\\something\\another folder\\";
    string[] filesArray = originalPath.Split(Path.AltDirectorySeparatorChar,
                                  Path.DirectorySeparatorChar);
    

    This should work regardless of the number of folders or the names.

提交回复
热议问题