创建子文件夹
Creating subfolder
我在转换时遇到了一些问题,我尝试转换有子文件夹的文件夹,但它没有创建子文件夹,只创建一个文件夹“_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);
我该如何解决这个问题?
您在循环的每次迭代中都创建了相同的文件夹。只需通过替换以下行来使用当前目录创建一个文件夹:
string folderPath = @"C:\test\" + "_converted";
folderPath = folderPath.Substring(folderPath.IndexOf(@"\") + 1);
这一行:
string folderPath = Path.Combine(@"C:\test\", dir + "_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)
{
// 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" 内结束
我在转换时遇到了一些问题,我尝试转换有子文件夹的文件夹,但它没有创建子文件夹,只创建一个文件夹“_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);
我该如何解决这个问题?
您在循环的每次迭代中都创建了相同的文件夹。只需通过替换以下行来使用当前目录创建一个文件夹:
string folderPath = @"C:\test\" + "_converted";
folderPath = folderPath.Substring(folderPath.IndexOf(@"\") + 1);
这一行:
string folderPath = Path.Combine(@"C:\test\", dir + "_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)
{
// 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" 内结束