如何检测给定主文件夹下的所有子文件夹?

发布于 2024-09-13 14:05:23 字数 368 浏览 5 评论 0原文

假设我的路径是 "c:/myapp/mainfolder/" 主文件夹中包含三个文件夹。 顺便说一句,它不需要识别主文件夹下的单独文件。

c:/myapp/mainfolder/subfolder1/
c:/myapp/mainfolder/subfolder2/
c:/myapp/mainfolder/subfolder3/

如何输入c:/myapp/mainfoder/ 并获取输出:string[] subArrFolders = {subfolder1, subfolder2, subfolder3}

C#2.0 使用。

谢谢。

Assume my path is "c:/myapp/mainfolder/"
there are three folder included in the main folder.
BTW, It doesn't need to identify separate files under mainfolder.

c:/myapp/mainfolder/subfolder1/
c:/myapp/mainfolder/subfolder2/
c:/myapp/mainfolder/subfolder3/

How can I input c:/myapp/mainfoder/
and get the output: string[] subArrFolders = {subfolder1, subfolder2, subfolder3}

C#2.0 using.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七分※倦醒 2024-09-20 14:05:23

您可以使用 Directory.GetDireatories() 获取 的子目录一条已知的路径。你可以这样使用它:

string MyPath = "c:\\myapp\\mainfolder\\";
string[] subArrFolders = IO.Directory.GetDiretories(MyPath);

You can use Directory.GetDireatories() to get the sub directories of a known path. You can use it like this:

string MyPath = "c:\\myapp\\mainfolder\\";
string[] subArrFolders = IO.Directory.GetDiretories(MyPath);
白鸥掠海 2024-09-20 14:05:23

由于缺乏更好的信息,这个答案假设他要求子文件夹名称,而不是完整路径名称,这将给您:

这将允许您提取叶文件夹名称:

using System;
using System.Text;
using System.IO;

namespace StackOverflow_NET
{
    class Program
    {
        static void Main(string[] args)
        {
            String path = @"C:\myapp\mainfolder";
            DirectoryInfo info = new DirectoryInfo(path);
            DirectoryInfo [] sub_directories = info.GetDirectories("*",SearchOption.AllDirectories);

            foreach (DirectoryInfo dir in sub_directories)
            {
                Console.WriteLine(dir.Name);
            }
        }
    }
}

输出:

subfolder1
subfolder2
subfolder3

这里的关键区别是 DirectoryInfo类允许您通过 Name 属性获取叶目录名称。

For lack of better information this answer assumes he asked for the sub-folder name, not the full path name, which is what that will give you:

This will allow you extract the leaf folder name:

using System;
using System.Text;
using System.IO;

namespace StackOverflow_NET
{
    class Program
    {
        static void Main(string[] args)
        {
            String path = @"C:\myapp\mainfolder";
            DirectoryInfo info = new DirectoryInfo(path);
            DirectoryInfo [] sub_directories = info.GetDirectories("*",SearchOption.AllDirectories);

            foreach (DirectoryInfo dir in sub_directories)
            {
                Console.WriteLine(dir.Name);
            }
        }
    }
}

Output:

subfolder1
subfolder2
subfolder3

The key difference here is the DirectoryInfo class allows you to get the leaf directory name via the Name property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文