I have a fairly simple task - from a collection of images I need to extract the Creation Date, the Last Modified Date and the Picture Taken Date and write them to a text file. For the Picture Taken Date, I am using metadata-extractor.
This is some sample code,
List<FileInfo> fileList = Utils.FileList(targetPath, true);
foreach (FileInfo fi in fileList)
{
string dateTime = "";
try
{
var metadatadir = ImageMetadataReader.ReadMetadata(fi.FullName);
var subIfdDirectory = metadatadir.OfType<ExifSubIfdDirectory>().FirstOrDefault();
dateTime = subIfdDirectory?.GetDescription(ExifDirectoryBase.TagDateTimeOriginal);
}
catch (Exception e)
{
Console.WriteLine(e);
}
DateTime creationDate = File.GetCreationTime(fi.FullName);
DateTime modifiedDate = File.GetLastWriteTime(fi.FullName);
string outputLine = "Filename " + fi.Name + "\t Creation Time: " + creationDate +
"\t Modified Time: " + modifiedDate + "\t" + "Exif Time: " + dateTime + "\n";
File.AppendAllText(targetFile, outputLine);
fileCount++;
}
I wrap a Stopwatch
object around this block to measure performance and this is the result I get,
Processed 2244 files in 440218ms.
If I comment out the Exif code (the try-catch
block) I get,
Processed 2244 files in 116928ms.
Am I using the library incorrectly? Is there a faster way of pulling the data out?
EDIT Based on feedback I have switched to using StreamWriter
as below,
using (StreamWriter tFile = File.AppendText(targetFile))
{
// Code
tFile.WriteLine(outputLine);
}
Based on this latest change the time taken has been cut in half with the Exif code,
Processed 2244 files in 212278ms.