C#我如何提取从路径上的每个文件夹名称? [英] C# How do I extract each folder name from a path?

查看:159
本文介绍了C#我如何提取从路径上的每个文件夹名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的路径是\\服务器\\ folderName1 \\另一名\\东西\\另一个文件夹\\

我如何提取每个文件夹名称为一个字符串,如果我不知道有多少文件夹有路径,我不知道这个文件夹名称?

非常感谢


解决方案

 字符串mypath中= @.. \\ folder1中\\文件夹2 \\文件夹2;
字符串[] =目录mypath.Split(Path.DirectorySeparatorChar);

编辑:
这将返回目录数组中的每个单独的文件夹中。你可以得到的文件夹数量返回是这样的:

  INT FOLDERCOUNT = directories.Length;

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 folders there are in the path and I don't know the folder names?

Many thanks

解决方案

string mypath = @"..\folder1\folder2\folder2";
string[] directories = mypath.Split(Path.DirectorySeparatorChar);

Edit: This returns each individual folder in the directories array. You can get the number of folders returned like this:

int folderCount = directories.Length;

这篇关于C#我如何提取从路径上的每个文件夹名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆