C# 有没有办法从所有类型的文件中提取所有元数据?
我试图从一堆文件中提取元数据。文件可以是图像、视频或任何类型。我想从文件中提取所有可用的元数据,而不考虑其类型。我尝试使用了C# 有没有办法从所有类型的文件中提取所有元数据?,c#,C#,我试图从一堆文件中提取元数据。文件可以是图像、视频或任何类型。我想从文件中提取所有可用的元数据,而不考虑其类型。我尝试使用了WindowsAPICodePack和Shell32。我能够提取一组属性,但我需要文件中可用的“投影类型”元数据。但是WindowsAPICodePack和Shell32都未能提取相同的内容。有什么解决办法吗 这是我试过的Shell 32代码 List<string> arrHeaders = new List<string>(); List<
WindowsAPICodePack
和Shell32
。我能够提取一组属性,但我需要文件中可用的“投影类型”元数据。但是WindowsAPICodePack
和Shell32
都未能提取相同的内容。有什么解决办法吗
这是我试过的Shell 32
代码
List<string> arrHeaders = new List<string>();
List<Tuple<int, string, string>> attributes = new List<Tuple<int, string, string>>();
Shell32.Shell shell = new Shell32.Shell();
var strFileName = @"C:\Users\Admin\Google Drive\image.jpg";
Shell32.Folder objFolder = shell.NameSpace(System.IO.Path.GetDirectoryName(strFileName));
Shell32.FolderItem folderItem = objFolder.ParseName(System.IO.Path.GetFileName(strFileName));
for (int i = 0; i < short.MaxValue; i++)
{
string header = objFolder.GetDetailsOf(null, i);
if (String.IsNullOrEmpty(header))
break;
arrHeaders.Add(header);
}
// The attributes list below will contain a tuple with attribute index, name and value
// Once you know the index of the attribute you want to get,
// you can get it directly without looping, like this:
var Authors = objFolder.GetDetailsOf(folderItem, 20);
for (int i = 0; i < arrHeaders.Count; i++)
{
var attrName = arrHeaders[i];
var attrValue = objFolder.GetDetailsOf(folderItem, i);
var attrIdx = i;
attributes.Add(new Tuple<int, string, string>(attrIdx, attrName, attrValue));
Debug.WriteLine("{0}\t{1}: {2}", i, attrName, attrValue);
}
Console.ReadLine();
好的,经过一番搜索,我最终使用了 该工具在提取大量元数据方面非常有效,尤其是我特别需要的“ProjectionType”。此属性用于检查上载的文件是否为360视频/图像。这就是我最后的结局
public List<Metadata> ExtractMetaDataExifTool(string filepath, string ExifToolPath)
{
#region ReadFromFile
string output = "";
var lstMetadata = new List<Metadata>();
using (var p = new Process())
{
// exiftool command
string toolPath = "";
toolPath += " -s ";
toolPath += "-fast -G -t -m -q -q -n ";
toolPath += "\"" + filepath + "\"";
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "\"" + ExifToolPath + "\\exiftool.exe" + "\"";
startInfo.Arguments = toolPath;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
p.StartInfo = startInfo;
bool status = p.Start();
StringBuilder q = new StringBuilder();
while (!p.HasExited)
{
q.Append(p.StandardOutput.ReadToEnd());
}
output = q.ToString();
p.WaitForExit();
}
#endregion ReadFromFile
#region ExtractFileMetadataFromString
while (output.Length > 0)
{
int epos = output.IndexOf('\r');
if (epos < 0)
epos = output.Length;
string tmp = output.Substring(0, epos);
int tpos1 = tmp.IndexOf('\t');
int tpos2 = tmp.IndexOf('\t', tpos1 + 1);
if (tpos1 > 0 && tpos2 > 0)
{
string taggroup = tmp.Substring(0, tpos1);
++tpos1;
string tagname = tmp.Substring(tpos1, tpos2 - tpos1);
++tpos2;
string tagvalue = tmp.Substring(tpos2, tmp.Length - tpos2);
// special processing for tags with binary data
tpos1 = tagvalue.IndexOf(", use -b option to extract");
if (tpos1 >= 0)
tagvalue.Remove(tpos1, 26);
if (!string.IsNullOrEmpty(taggroup) && !string.IsNullOrEmpty(tagname) && !string.IsNullOrEmpty(tagvalue))
{
lstMetadata.Add(new Metadata
{
group = taggroup?.Trim(),
name = tagname?.Trim(),
value = tagvalue?.Trim()
});
}
}
// is \r followed by \n ?
if (epos < output.Length)
epos += (output[epos + 1] == '\n') ? 2 : 1;
output = output.Substring(epos, output.Length - epos);
}
#endregion ExtractFileMetadataFromString
return lstMetadata;
}
public List ExtractMetaDataExifTool(字符串文件路径,字符串ExifToolPath)
{
#区域读取文件
字符串输出=”;
var lstmatadata=新列表();
使用(var p=new Process())
{
//exiftool命令
字符串刀具路径=”;
刀轨+=“-s”;
刀轨+=“-fast-G-t-m-q-q-n”;
工具路径+=“\”+文件路径+“\”;
System.Diagnostics.ProcessStartInfo startInfo=新系统.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle=System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName=“\”“+ExifToolPath+”\\exiftool.exe“+”\”;
startInfo.Arguments=刀具路径;
startInfo.RedirectStandardOutput=true;
startInfo.UseShellExecute=false;
p、 StartInfo=StartInfo;
bool status=p.Start();
StringBuilder q=新的StringBuilder();
当(!p.hasExit)退出时
{
q、 追加(p.StandardOutput.ReadToEnd());
}
输出=q.ToString();
p、 WaitForExit();
}
#endregion ReadFromFile
#区域提取FileMetadataFromString
while(output.Length>0)
{
int epos=output.IndexOf('\r');
如果(epos<0)
epos=输出长度;
字符串tmp=output.Substring(0,epos);
int tpos1=tmp.IndexOf('\t');
int tpos2=tmp.IndexOf('\t',tpos1+1);
如果(tpos1>0&&tpos2>0)
{
string taggroup=tmp.Substring(0,tpos1);
++tpos1;
字符串标记名=tmp.Substring(tpos1,tpos2-tpos1);
++tpos2;
string tagvalue=tmp.Substring(tpos2,tmp.Length-tpos2);
//二进制数据标签的特殊处理
tpos1=tagvalue.IndexOf(“,使用-b选项提取”);
如果(tpos1>=0)
tagvalue.Remove(tpos1,26);
如果(!string.IsNullOrEmpty(标记组)和&!string.IsNullOrEmpty(标记名)和&!string.IsNullOrEmpty(标记值))
{
添加(新元数据)
{
组=标记组?.Trim(),
name=标记名?.Trim(),
value=tagvalue?.Trim()
});
}
}
//后面是\r\n吗?
if(epos<输出长度)
epos+=(输出[epos+1]='\n')?2:1;
output=output.Substring(epos,output.Length-epos);
}
#endregion ExtractFileMetadataFromString
返回元数据;
}
public List<Metadata> ExtractMetaDataExifTool(string filepath, string ExifToolPath)
{
#region ReadFromFile
string output = "";
var lstMetadata = new List<Metadata>();
using (var p = new Process())
{
// exiftool command
string toolPath = "";
toolPath += " -s ";
toolPath += "-fast -G -t -m -q -q -n ";
toolPath += "\"" + filepath + "\"";
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "\"" + ExifToolPath + "\\exiftool.exe" + "\"";
startInfo.Arguments = toolPath;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
p.StartInfo = startInfo;
bool status = p.Start();
StringBuilder q = new StringBuilder();
while (!p.HasExited)
{
q.Append(p.StandardOutput.ReadToEnd());
}
output = q.ToString();
p.WaitForExit();
}
#endregion ReadFromFile
#region ExtractFileMetadataFromString
while (output.Length > 0)
{
int epos = output.IndexOf('\r');
if (epos < 0)
epos = output.Length;
string tmp = output.Substring(0, epos);
int tpos1 = tmp.IndexOf('\t');
int tpos2 = tmp.IndexOf('\t', tpos1 + 1);
if (tpos1 > 0 && tpos2 > 0)
{
string taggroup = tmp.Substring(0, tpos1);
++tpos1;
string tagname = tmp.Substring(tpos1, tpos2 - tpos1);
++tpos2;
string tagvalue = tmp.Substring(tpos2, tmp.Length - tpos2);
// special processing for tags with binary data
tpos1 = tagvalue.IndexOf(", use -b option to extract");
if (tpos1 >= 0)
tagvalue.Remove(tpos1, 26);
if (!string.IsNullOrEmpty(taggroup) && !string.IsNullOrEmpty(tagname) && !string.IsNullOrEmpty(tagvalue))
{
lstMetadata.Add(new Metadata
{
group = taggroup?.Trim(),
name = tagname?.Trim(),
value = tagvalue?.Trim()
});
}
}
// is \r followed by \n ?
if (epos < output.Length)
epos += (output[epos + 1] == '\n') ? 2 : 1;
output = output.Substring(epos, output.Length - epos);
}
#endregion ExtractFileMetadataFromString
return lstMetadata;
}