Reputation: 8172
If you right click a file in Windows explorer and select Properties, a dialog pops up with a ton of information. How can I access this information using C#?
Specifically, I'm looking for the information on the Details tab. I'm working mostly with images, so "Date taken" is important. I'll be working with other files as well, so it would be nice to have a way to get any metadata associated with the file.
I've seen places mention using Shell32.dll to get this information, but I get errors when I try to reference this library. Is there another way to do this, maybe through P/Invoke?
Upvotes: 0
Views: 1693
Reputation: 3397
After messing around a little, here is a VERY limited example of how to get the image meta data.
var image = System.Drawing.Image.FromFile(@"C:\your\image\here");
foreach (var a in image.PropertyItems)
{
dynamic value;
switch (a.Type)
{
case 2:
value = Encoding.ASCII.GetString(a.Value);
break;
case 3:
value = BitConverter.ToInt16(a.Value, 0);
break;
case 4:
value = BitConverter.ToInt32(a.Value, 0);
break;
default:
value = "NaN";
break;
}
Console.WriteLine("Type: {0} \r\n Value: {1}", a.Type, value);
}
You can find some more information on Microsoft's site and search on the Image Metadata specifications to fully write one yourself.
There is always the option of using a third-party library that already handles this, but I don't do much work in the area of images myself.
Upvotes: 1
Reputation: 12439
You can use File
class methods for this purpose:
File.GetCreationTime(filename)
File.GetLastWriteTime(filename)
File.GetLastAccessTime(filename)
//and many more in the intellisense
Upvotes: 1
Reputation: 4190
Have you tried the FileSystemInfo class?
http://msdn.microsoft.com/en-us/library/system.io.filesysteminfo.aspx
Upvotes: 1