C# 如何在包含jpg文件的每个目录和子目录中创建文件夹?

C# 如何在包含jpg文件的每个目录和子目录中创建文件夹?,c#,for-loop,directory,C#,For Loop,Directory,我需要能够在主路径的每个目录和子目录中创建一个文件夹 但我不知道如何获取包含文件的每个路径,以便在那里创建文件夹。。。实际上,我让所有目录都这样做: using System; using System.IO; using System.Collections.Generic; using System.Linq; using System.Text; using ImageMagick; using System.Collections;

我需要能够在主路径的每个目录和子目录中创建一个文件夹

但我不知道如何获取包含文件的每个路径,以便在那里创建文件夹。。。实际上,我让所有目录都这样做:

    using System;
    using System.IO;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ImageMagick;
    using System.Collections;

    namespace ImgOptimization
    {
        class Program
        {
            static void Main(string[] args)
            {


                string path = "mypath";



                if (System.IO.Directory.Exists(path))
                {

                    string[] subdirectorios = System.IO.Directory.GetFiles(path, "*.jpg", SearchOption.AllDirectories);

                    string carpetas = null;



                    for (int i = 0; i < subdirectorios.Length; i++)
                    {

                        carpetas = subdirectorios[i];


                            string newpathMobile = "newpath"


                            if (!System.IO.Directory.Exists(newpath))
                            {
                                System.IO.Directory.CreateDirectory(newpath);
                            }

                    }
                }
}
}
使用系统;
使用System.IO;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用ImageMagick;
使用系统集合;
命名空间优化
{
班级计划
{
静态void Main(字符串[]参数)
{
string path=“mypath”;
if(System.IO.Directory.Exists(path))
{
string[]subdirectory ios=System.IO.Directory.GetFiles(路径“*.jpg”,SearchOption.AllDirectories);
字符串as=null;
for(int i=0;i<子目录ios.Length;i++)
{
地毯=子目录[i];
字符串newpathMobile=“newpath”
如果(!System.IO.Directory.Exists(newpath))
{
System.IO.Directory.CreateDirectory(newpath);
}
}
}
}
}

但我不知道如何在循环过程中在每个路径中创建文件夹,知道如何实现吗?

您可以递归地遍历所有子目录,如下所示

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace TestSubDirectories
{
    class Program
    {

        static string NewFolderName = "NewFolder";
        static void Main(string[] args)
        {

            string path = "C:\\testfolder";

            if (Directory.Exists(path))
                CreateSubFolderIn(path);
        }


        static void CreateSubFolderIn(string path)
        {
            foreach (string SubDirectory in Directory.GetDirectories(path))
            {
                CreateSubFolderIn(SubDirectory);
            }

            if(!Directory.Exists(path + "\\" + NewFolderName))
                Directory.CreateDirectory(path + "\\" + NewFolderName);

        }

    }
}

一种相当简单的方法是使用
DirectoryInfo
类,该类返回强类型的
DirectoryInfo
对象(而不仅仅是表示目录路径的字符串)

我们可以筛选目录中包含具有指定扩展名的文件的结果,然后我们可以向该目录添加子目录:

public static void CreateDirectoryNextToFileType(string rootPath, string fileExtension, 
    string newDirectoryName)
{
    if (rootPath == null || !Directory.Exists(rootPath))
        throw new ArgumentException("rootPath must be the path to an existing directory");

    if (fileExtension == null) throw new ArgumentNullException(nameof(fileExtension));

    if (string.IsNullOrEmpty(subDirectoryName) ||
        subDirectoryName.Any(chr => Path.GetInvalidPathChars().Contains(chr)))
        throw new ArgumentException("subDirectoryName is null, empty, or " + 
            "contains invalid characters");

    // Enumerate all directories and return those that 
    // contain a file with the specified extension
    var directoriesContainingFile = new DirectoryInfo(rootPath)
        .EnumerateDirectories("*", SearchOption.AllDirectories)
        .Where(dir => dir.EnumerateFiles().Any(file =>
            file.Extension.Equals(fileExtension, StringComparison.OrdinalIgnoreCase)));

    // For each of the directories, create a sub directory with the specified name
    foreach (var directory in directoriesContainingFile)
    {
        directory.CreateSubdirectory(newDirectoryName);
    }
}
因此,如果我们想在根
“f:\private\temp”
下包含
“.cmd”
文件的任何目录中创建一个名为
“newdirnextotcmdfile”
的子目录,我们将调用以下方法:

CreateDirectoryNextToFileType(@"f:\private\temp", ".cmd", "NewDirNextToCmdFile");