gpt4 book ai didi

c# - C# 获取子文件夹名称的方法

转载 作者:行者123 更新时间:2023-12-02 18:08:57 25 4
gpt4 key购买 nike

我有一个类似“c:/videos”的文件夹路径。它包含汽车、自行车、公共(public)汽车等子文件夹。只需获取子文件夹名称并将其存储在字符串数组中。

请注意,我不需要完整的子文件夹路径

需要的东西,例如:- 汽车、自行车、公共(public)汽车

不像 c:/videos/carc:/视频/自行车c:/视频/总线

最佳答案

您必须迭代子目录。
并将起始路径 c:\videos 替换为空字符串:

var rootDir = @"c:\videos";
DirectoryInfo directoryInfo = new DirectoryInfo(rootDir);

var dirs = new System.Collections.Generic.List<string>();
foreach (var dir in directoryInfo.GetDirectories())
{
dirs.Add(dir.Name.Replace($"{rootDir}\\", ""));
}
var result = dirs.ToArray();

enter image description here

关于c# - C# 获取子文件夹名称的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72713609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com