创建子文件夹

时间:2016-07-28 05:07:32

标签: c# subdirectory

转换时我没有什么问题,我尝试将文件夹转换为子文件夹但不创建子文件夹,只创建一个文件夹“_converted”,并在文件夹中转换所有子文件夹图像。

我的代码:

private void btnConvert_Click(object sender, EventArgs e)
{
    string[] originalImage = Directory.GetDirectories(txtFilePath.Text, "*.*", 
                                                            SearchOption.AllDirectories);
    foreach (var directory in originalImage)
    {
        Debug.WriteLine(directory);
    }
    foreach (string dir in originalImage)
    {

        string folderPath = @"C:\test\" + "_converted";
        folderPath = folderPath.Substring(folderPath.IndexOf(@"\") + 1);
        DirectoryInfo di = Directory.CreateDirectory(folderPath);
        if (Directory.Exists(folderPath))
        {
            DirectoryInfo dInfo = new DirectoryInfo(dir);
            foreach (var filename in dInfo.GetFiles())
            {

                FileInfo fInfo = new FileInfo(filename.FullName);
                var fileExtension = fInfo.Extension;
                var fileOriginalDate = fInfo.CreationTime;

                if (fileExtension.ToUpper() == ".JPG" || fileExtension.ToUpper() == ".PNG")
                {
                    using (Bitmap bitmap = new Bitmap(filename.FullName))
                    {
                        string fn = Path.GetFileNameWithoutExtension(filename.FullName);
                        VariousQuality(bitmap, fn, fileExtension, 
                                              fileOriginalDate, folderPath);
                    }
                }
            }
        }
    }
}

我尝试使用这种方法:

folderPath = folderPath.Substring(folderPath.IndexOf(@"\") + 1);

我如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

您在循环的每次迭代中创建相同的文件夹。只需使用当前目录创建一个文件夹,方法是替换以下行:

string folderPath = @"C:\test\" + "_converted";
folderPath = folderPath.Substring(folderPath.IndexOf(@"\") + 1);

这一行:

string folderPath = Path.Combine(@"C:\test\", dir + "_converted");

答案 1 :(得分:0)

您没有处理文件夹'名字正确。试试这个:

private void btnConvert_Click(object sender, EventArgs e)
{
    string[] originalImage = Directory.GetDirectories(txtFilePath.Text, "*.*", SearchOption.AllDirectories);

    foreach (var directory in originalImage)
    {
        Debug.WriteLine(directory);
    }
    foreach (string dir in originalImage)
    {
        // The name of the current folder (dir)
        // This will convert "C:\Users\User\Desktop\Myfolder\Image1" to simply "Image1" since we create a substring after the LAST backslash ('\')
        string folderName = dir.Substring(dir.LastIndexOf('\\') + 1); // Ex. "Image1"

        // This will now be "C:\test\FOLDERNAME_converted"
        string folderPath = @"C:\test\" + folderName + @"_converted\"; // Ex. "C:\test\image1_converted\";

        // This can now create the folders
        DirectoryInfo di = Directory.CreateDirectory(folderPath);

        // Below is unchanged for now
        if (Directory.Exists(folderPath))
        {
            DirectoryInfo dInfo = new DirectoryInfo(dir);
            foreach (var filename in dInfo.GetFiles())
            {
                FileInfo fInfo = new FileInfo(filename.FullName);
                var fileExtension = fInfo.Extension;
                var fileOriginalDate = fInfo.CreationTime;

                if (fileExtension.ToUpper() == ".JPG" || fileExtension.ToUpper() == ".PNG")
                {
                    using (Bitmap bitmap = new Bitmap(filename.FullName))
                    {
                        string fn = Path.GetFileNameWithoutExtension(filename.FullName);
                        VariousQuality(bitmap, fn, fileExtension,
                                              fileOriginalDate, folderPath);
                    }
                }
            }
        }
    }
}

我希望这有帮助。

我只有一个问题。获取目录路径(txtFilePath.Text)中的目录时,您将获得所有文件夹,包括子文件夹(SearchOptions.AllDirectories)。将已转换的文件夹保存到" C:\test"文件夹,您没有考虑到文件夹可能是一个子文件夹。因此,会发生以下问题。假设您有一个包含文件夹的文件夹:

"HeadFolder -> Image1 -> Image1.2"

该计划将找到什么:

1. "Path\\To\\Image1"
2. "Path\\To\\Image1.2"

转换后,您将获得:

"HeadFolder"
    "Image1"
    "Image1.2"

请注意" Image1.2"不会在内部结束" Image1"在转换之前