簡體   English   中英

C# WindowsApiCodepack PropertySystem AccessViolationException

[英]C# WindowsApiCodepack PropertySystem AccessViolationException

使用 WindowsAPICodePack 在 Win8/64 位上做一些資源管理器/shell 的事情。 在使用 x64 平台目標迭代文件屬性時,屬性系統存在一些問題導致 AccessViolationException。 似乎是 PropVariant.cs 中的一些問題。 切換到 x86 可以解決問題,但會導致目錄列表不完整(例如 system32/drivers 中缺少“etc”)。 有任何想法嗎?

using System;
using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack.Shell.PropertySystem;

namespace ApiCodepackTest
{
    class Program
    {
        const string path = @"c:\windows\system32\drivers";
        static void Main(string[] args)
        {
            var shellObject = (ShellFolder)ShellObject.FromParsingName(path);
            showProperties(shellObject);
            showItems(shellObject);
            Console.ReadLine();
        }

        static void showProperties(ShellFolder folder)
        {
            var sys = folder.Properties.System;
            foreach (var prop in sys.GetType().GetProperties())
            {
                try
                {
                    var shellProperty = prop.GetValue(sys) as IShellProperty;
                    if (shellProperty != null && shellProperty.ValueAsObject != null)
                        Console.WriteLine(shellProperty.CanonicalName + " " + shellProperty.ValueAsObject);
                }
                catch{} //you should not pass!
            }
        }

        static void showItems(ShellFolder folder)
        {
            foreach (var i in folder)
                Console.WriteLine(i.Name);
        }
    }

我不是真的喜歡pinvoke和c ++的東西,但是我在PropVariant.cs中用一點修復重新編譯了源代碼

//[FieldOffset(12)] original
    [FieldOffset(16)]
    IntPtr _ptr2;

這解決了這個問題

對於發現此問題的任何其他人,您現在可以避免重新編譯源代碼來實現此錯誤修復。

本回答中所述,開源 WindowsAPICodePack 現在由不同的開發人員 (Pierre Sprimont) 維護。 它最近於 2022 年 7 月更新,可在 GitHub 上獲得,或作為一系列 Nuget 包直接通過 Visual Studio 獲得。 討論他的項目分支的開發者網站在這里,它有指向每個 Github 回購和 Nuget 頁面的鏈接。

在@Aleksey接受的答案中討論的PropVariant.cs 中的 AccessViolationException 錯誤已得到修復,還有許多其他錯誤修復和改進(包括對 .NET Core 的支持)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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