在文件夹中创建多个子文件夹

时间:2018-03-01 10:01:22

标签: c#

我想一次在文件夹中创建多个子文件夹。 Multiple Folders

与图像一样,我想在B中一次创建A-> B-> C和D而没有循环。有没有办法在C#

中实现它

3 个答案:

答案 0 :(得分:1)

Directory.CreateDirectory将创建给定路径中的所有目录,包括所有子目录。

using System.IO;

var paths = new [] { "F:\\A\\B\\C", "F:\\A\\B\\D" };

foreach (var path in paths) {
    try {
        // Determine whether the directory exists.
        if (Directory.Exists(path)) {
            Console.WriteLine($"Skipping path '{path}' because it exists already.");
            continue;
        }

        // Try to create the directory.
        var di = Directory.CreateDirectory(path);
        Console.WriteLine($"Created path '{path}' successfully at {Directory.GetCreationTime(path)}.");
    }
    catch (Exception e) {
        Console.WriteLine($"The process failed: {e}");
    }
}

答案 1 :(得分:0)

尝试此代码。

 private string GetUploadFileFolderPath()
    {

        string struploadUserImageFolderPath ="~/A/";
        string strGetStockUploadFolderName ="C";
        string strfullFolderPath = "~/A/" + "B" + "/" + strGetStockUploadFolderName + "/";
        return strfullFolderPath;

    }

 struploadUserImageFolderPath = GetUploadFileFolderPath();    // file path
                    if (!Directory.Exists(Server.MapPath(struploadUserImageFolderPath)))
                    {
                        Directory.CreateDirectory(Server.MapPath(struploadUserImageFolderPath));
                    }

答案 2 :(得分:0)

var path = @"PATH_TO_FILE"; new FileInfo(path).Directory.Create();