正在创建子文件夹

本文关键字:文件夹 创建 | 更新日期: 2023-09-27 17:58:57

我在转换方面没有什么问题,我尝试转换文件夹,其中有子文件夹,但它没有创建子文件夹,只使一个文件夹"_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);

我该如何解决这个问题?

正在创建子文件夹

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

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"中

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

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

使用此行:

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