[英]How to get Root Drive details using Windows API Code Pack?
我编写了函数,使我可以获取任何文件的所有属性。
此功能甚至适用于文件夹。
但是我提到,如果没有“ Windows API代码包”并使用“ Shell32”,您可以尽早获得任何文件的308个属性。
Shell32.Shell shell = new Shell32.Shell();
shell.NameSpace(@"C:\_test\clip.mp4").GetDetailsOf(null, i); // i = 0...320
但是,使用“ Windows API代码包”时,“ DefaultPropertyCollection”集合中只有56个属性。
例如,此集合中没有“评级”属性。
然后我意识到,我应该查看“ shellFile.Properties.System”而不是“ shellFile.Properties.DefaultPropertyCollection”。
private void GetFileDatails(string path)
{
var shellFile = ShellFile.FromParsingName(path);
textBox1.Text += shellFile.Properties.DefaultPropertyCollection.Count + "\r\n";
shellFile.Properties.DefaultPropertyCollection.ToList().ForEach(el => textBox1.Text += el.Description.CanonicalName + " - " + el.Description.DisplayName + " = " + el.ValueAsObject + "\r\n");
textBox1.Text += "\r\n" + shellFile.Properties.System.Rating;
}
是的,这是真的!
但是,如何使用此“ shellFile.Properties.System”获取文件的其他200多个属性?
它甚至没有收藏! 这不是IEnumerable! 您不能使用foreach循环!
您应该始终输入
"shellFile.Properties.System.*" //you should type this line more then 100 times
无论如何,我也无法获得根驱动器的详细信息!
使用“ Windows API代码包”或“ Shell32”,我无法获得任何分区的“ DriveFormat”! 请! 告诉我如何获取“ C盘”详细信息?
您可以使用Reflection枚举所有属性并按以下方式获取它们的值
static void Main(string[] args)
{
string fileName = Path.Combine(Directory.GetCurrentDirectory(), "All Polished.mp4");
ShellObject shellObject= ShellObject.FromParsingName(fileName);
PropertyInfo[] propertyInfos = shellObject.Properties.System.GetType().GetProperties();
foreach (var propertyInfo in propertyInfos)
{
object value = propertyInfo.GetValue(shellObject.Properties.System, null);
if (value is ShellProperty<int?>)
{
var nullableIntValue = (value as ShellProperty<int?>).Value;
Console.WriteLine($"{propertyInfo.Name} - {nullableIntValue}");
}
else if (value is ShellProperty<ulong?>)
{
var nullableLongValue =
(value as ShellProperty<ulong?>).Value;
Console.WriteLine($"{propertyInfo.Name} - {nullableLongValue}");
}
else if (value is ShellProperty<string>)
{
var stringValue =
(value as ShellProperty<string>).Value;
Console.WriteLine($"{propertyInfo.Name} - {stringValue}");
}
else if (value is ShellProperty<object>)
{
var objectValue =
(value as ShellProperty<object>).Value;
Console.WriteLine($"{propertyInfo.Name} - {objectValue}");
}
else
{
Console.WriteLine($"{propertyInfo.Name} - Dummy value");
}
}
Console.ReadLine();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.