You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF中SHBrowseForFolder新UI创建文件夹时程序崩溃问题

问题:启用BIF_USENEWUI标志后点击“创建新文件夹”导致WPF程序崩溃

我在WPF应用中集成了一段代码,原本能正常获取选中文件夹的路径。但设置bi.ulFlags = 0x00000040;(即BIF_USENEWUI,启用新UI样式)后,界面出现“创建新文件夹”按钮,点击该按钮程序直接崩溃,即使加了try...catch也捕获不到任何异常。只有这个场景会崩溃,目前已经切换回旧UI标志来隐藏该选项。

代码片段

public static class NativeMethods
{
    [DllImport("shell32.dll")]
    public static extern IntPtr SHBrowseForFolder(ref BROWSEINFO lpbi);

    [DllImport("shell32.dll", CharSet = CharSet.Auto)]
    public static extern bool SHGetPathFromIDList(IntPtr pidl, StringBuilder pszPath);

    [DllImport("shell32.dll")]
    public static extern void CoTaskMemFree(IntPtr pv);

    [StructLayout(LayoutKind.Sequential)]
    public struct BROWSEINFO
    {
        public IntPtr hwndOwner;
        public IntPtr pidlRoot;
        public IntPtr pszDisplayName;
        public string lpszTitle;
        public uint ulFlags;
        public IntPtr lpfn;
        public IntPtr lParam;
        public int iImage;
    }
}


private string SelectFolder(string windowTitle)
{
    IntPtr buffer = Marshal.AllocHGlobal(260 * Marshal.SystemDefaultCharSize);
    try
    {
        NativeMethods.BROWSEINFO bi = new NativeMethods.BROWSEINFO();
        bi.hwndOwner = new WindowInteropHelper(Application.Current.MainWindow).Handle;
        bi.lpszTitle = windowTitle;
        bi.ulFlags = 0x00000040;//bi.ulFlags = 0x00000000; //bi.ulFlags = 0x00000040; // BIF_USENEWUI, Enable the new UI style

        IntPtr pidl = NativeMethods.SHBrowseForFolder(ref bi);
        StringBuilder path = new StringBuilder(260);

        if (pidl != IntPtr.Zero)
        {
            bool success = NativeMethods.SHGetPathFromIDList(pidl, path);

            if (success)
            {
                string folderPath = path.ToString();
                return folderPath;
            }                    
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error: " + ex.Message); 
    }
    finally
    {
        Marshal.FreeHGlobal(buffer);
    }

    return null; 
}

Windows应用程序事件查看器错误信息

错误应用程序名称: app.exe,版本: 1.0.0.0,时间戳: 0x8f5958c0
错误模块名称: unknown,版本: 0.0.0.0,时间戳: 0x00000000
异常代码: 0xc000041d
错误偏移量: 0x04c00002
错误进程 ID: 0x31d8
错误应用程序启动时间: 0x01d9c0600eb1538e
错误应用程序路径: C:\app.exe
错误模块路径: unknown
报告 ID: 1f791fc1-e8c6-481e-af3a-5a2a80d70d8c
错误包全名:
错误包相对应用程序 ID:

内容的提问来源于stack exchange,提问作者Dev1n.101

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 19:10:19