我的目的是选择一个文件夹并读取其中视频和图像的创建时间。我可以对带有位图的图像执行此操作,但在视频上出现错误。这是给您的示例代码。
string[] directoryList = Directory.GetFiles(label1.Text);
foreach (string file in directoryList) {
// Create an Image object using the image path
using (var image = new Bitmap(file))
{
// Get the image property items (metadata)
PropertyItem[] propertyItems = image.PropertyItems;
string savedDate = null;
// Iterate over the property items and display the information
foreach (PropertyItem propertyItem in propertyItems)
{
int type = propertyItem.Type;
byte[] value = propertyItem.Value;
if (type == 2) // ASCII string
{
string date = Encoding.ASCII.GetString(value);
if (Regex.IsMatch(date, "([0-9]+(:[0-9]+)+)\\s([0-9]+(:[0-9]+)+)") == true)
{
date = date.Substring(0, 7).Replace(":", "-");
savedDate = date;
MessageBox.Show(savedDate);
break;
}
}
else if (type == 7) // Undefined binary data
{
// Process the binary data as needed
// Example: Console.WriteLine($"Property Value (Binary): {BitConverter.ToString(value)}");
}
}
//FOLDER CREATING
if (!Directory.Exists(label1.Text))
{
Directory.CreateDirectory(label1.Text);
}
}
}
在尝试重新发明轮子之前,看看这个库是否有帮助:MetadataExtractor