0
我正在編寫一個C#應用程序,它可以掃描.jpg圖像的目錄。 然後更新圖像的Exif元信息。 完成此操作後,它會將新圖像保存到與獲取圖像的位置相同的文件夾結構中。如何在C中移動保存文件夾結構的文件夾#
一切工作正常,直到我嘗試並保存圖像保持與我從中獲取圖像相同的文件夾文件夾。
正在發生的事情是圖像被保存到一個文件夾而不是多個文件夾。
的文件夾結構,我得到的圖像看起來是這樣的:
1997 -> JAN-JUN 1997 -> APRIL 1997 -> 7.4.97 - 11.4.97 -> FRI 11.4.97
下面是處理任務的代碼的副本。
/// <summary>
/// Search the directories
/// </summary>
/// <param name="sDir">Pass in the directory to be searched</param>
void DirSearch(string sDir)
{
try
{
foreach (string d in Directory.GetDirectories(sDir))
{
foreach (string f in Directory.GetFiles(d, txtfile.Text))
{
if (progressBar1.Value < progressBar1.Maximum)
{
// Return the folder name - only the data part e.g 7.9.1997
string folderName = new FileInfo(f).Directory.Name;
string[] parts = folderName.Split(' ');
string lastPart = parts[parts.Length - 1];
// minimally loads image and closes it
ExifPropertyCollection properties = ExifReader.GetExifData(f.ToString());
int lastDot = imagePath.LastIndexOf('.');
string inputPath = f.ToString();
string outputPath = destinationDir + getFileNameFromPathString(f.ToString()) + ".jpg";
// add copyright tag
ExifProperty copyright = new ExifProperty();
copyright.Tag = ExifTag.Copyright;
copyright.Value = String.Format(
"Copyright (c){0} Shepparton News. All rights reserved.",
DateTime.Now.Year);
// Add folder date to exif tag
ExifProperty folderDate = new ExifProperty();
folderDate.Tag = ExifTag.DateTime;
folderDate.Value = lastPart.ToString();
ExifWriter.AddExifData(inputPath, outputPath, copyright, folderDate);
ListViewItem item = lstFilesFound.Items.Add(getFileNameFromPathString(f.ToString()));
item.SubItems.Add(lastPart.ToString());
item.SubItems.Add(f.ToString());
progressBar1.PerformStep();
}
}
DirSearch(d);
}
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
}
}
任何人都可以請指點我正確的方向或提供一個關於如何實現這一任務的例子。 任何幫助非常感謝。謝謝。
下面是一些輔助方法:
/// <summary>
/// Return the name of a file
/// </summary>
/// <param name="filePathString">The filepathstring to search</param>
public static string getFileNameFromPathString(string filePathString)
{
filePathString = filePathString.Replace("\\", "/");
string[] pathParts = Regex.Split(filePathString, "/");
return pathParts[pathParts.Length - 1];
}
圖片保存方法:
public static void AddExifData(string inputPath, string outputPath, params ExifProperty[] properties)
{
using (Image image = Image.FromFile(inputPath))
{
ExifWriter.AddExifData(image, new ExifPropertyCollection(properties));
image.Save(outputPath);
}
}
下面是新方法,我嘗試使用,但它給我的錯誤「最好的'System.Image.Drawing.Image.Save(string)'的重載方法匹配有一些無效參數。
public static void AddExifData(string inputPath, DirectoryInfo outputPath, params ExifProperty[] properties)
{
if (!outputPath.Exists)
{
outputPath.Create();
}
using (Image image = Image.FromFile(inputPath))
{
ExifWriter.AddExifData(image, new ExifPropertyCollection(properties));
image.Save(outputPath);
}
}
新AddExifData方法
public static void AddExifData(string inputPath, string outputPath, params ExifProperty[] properties)
{
if (!Directory.Exist(outputPath)) { Directory.Create(outputPath); }
using (Image image = Image.FromFile(inputPath))
{
ExifWriter.AddExifData(image, new ExifPropertyCollection(properties));
image.Save(outputPath.ToString(), ImageFormat.Jpeg);
}
}
上面這條新的方法給了我這些錯誤。 System.IO.Directory不包含「存在」 System.IO.Directory不包含「創建」
這是否方法替換現有DirSearch方法?它給出了錯誤:名稱'd'在當前上下文中不存在。 – Jason 2011-06-15 04:28:11
應該是字符串newRelative = relativeDir + relativeDir.Substring(lst,relativeDir.Length - lst)+「\\」;? – Jason 2011-06-15 04:29:26
現在試試........ – 2011-06-15 10:51:43