2015-09-26 62 views
-1

我写的函数允许我获取任何文件的所有属性。
该功能甚至适用于文件夹。如何使用Windows API代码包获取根驱动器详细信息?

但我提到,如果没有“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.DefaultPropertyCollection”我应该看看“shellFile.Properties.System”

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 Drive”细节?

+0

如何获得任何驱动器“的DriveType”属性?无论是不是“固定”类型? – IremadzeArchil19910311

+0

https://msdn.microsoft.com/en-us/library/system.io.driveinfo.aspx – Ben

回答

-1

您可以使用反射来枚举所有特性,并得到它们的值如下

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(); 
} 
相关问题