簡體   English   中英

如何使用Windows API代碼包獲取根驅動器詳細信息?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM