gpt4 book ai didi

c# - 尝试使用 C# 中的新 IFileDialog 和 IFileOpenDialog 接口(interface)以最少的代码打开文件对话框

转载 作者:太空狗 更新时间:2023-10-30 00:28:03 30 4
gpt4 key购买 nike

<分区>

我正在尝试使用 C# 和 Visual Studio 2010 中的 IFileOpenDialog 界面显示一个可以选择文件夹的标准打开文件对话框。

我试图使用最少的代码,所以我只在接口(interface)中定义了我需要的方法:

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// Disable warning CS0108: 'x' hides inherited member 'y'. Use the new keyword if hiding was intended.
\#pragma warning disable 0108

namespace FolderDialog
{

internal static class IIDGuid
{
internal const string IModalWindow = "b4db1657-70d7-485e-8e3e-6fcb5a5c1802";
internal const string IFileDialog = "42f85136-db7e-439c-85f1-e4075d135fc8";
internal const string IFileOpenDialog = "d57c7288-d4ad-4768-be02-9d969532d960";
}

internal static class CLSIDGuid
{
internal const string FileOpenDialog = "DC1C5A9C-E88A-4dde-A5A1-60F82A20AEF7";
}

static class NativeMethods
{

[Flags]
internal enum FOS : uint
{
FOS_PICKFOLDERS = 0x00000020
}

}

[ComImport(),
Guid(IIDGuid.IModalWindow),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IModalWindow
{

[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime),
PreserveSig]
int Show([In] IntPtr parent);
}

[ComImport(),
Guid(IIDGuid.IFileDialog),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IFileDialog : IModalWindow
{
// Defined on IModalWindow - repeated here due to requirements of COM interop layer
// --------------------------------------------------------------------------------
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime),
PreserveSig]
int Show([In] IntPtr parent);

[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void SetOptions([In] NativeMethods.FOS fos);
}

[ComImport(),
Guid(IIDGuid.IFileOpenDialog),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IFileOpenDialog : IFileDialog
{
// Defined on IModalWindow - repeated here due to requirements of COM interop layer
// --------------------------------------------------------------------------------
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime),
PreserveSig]
int Show([In] IntPtr parent);

[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void SetOptions([In] NativeMethods.FOS fos);
}


// ---------------------------------------------------
// .NET classes representing runtime callable wrappers
[ComImport,
ClassInterface(ClassInterfaceType.None),
TypeLibType(TypeLibTypeFlags.FCanCreate),
Guid(CLSIDGuid.FileOpenDialog)]
internal class FileOpenDialogRCW
{
}

// ---------------------------------------------------------
// Coclass interfaces - designed to "look like" the object
// in the API, so that the 'new' operator can be used in a
// straightforward way. Behind the scenes, the C# compiler
// morphs all 'new CoClass()' calls to 'new CoClassWrapper()'
[ComImport,
Guid(IIDGuid.IFileOpenDialog),
CoClass(typeof(FileOpenDialogRCW))]
internal interface NativeFileOpenDialog : IFileOpenDialog
{
}

如果我只打电话

        IFileDialog dialog = null;
try
{
dialog = new NativeFileOpenDialog();
dialog.Show(IntPtr.Zero);
}
finally
{
if (dialog != null)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(dialog);
}

它工作正常,打开文件对话框没有任何错误。

如果我尝试:

        IFileDialog dialog = null;
try
{
dialog = new NativeFileOpenDialog();
dialog.SetOptions(NativeMethods.FOS.FOS_PICKFOLDERS);
dialog.Show(IntPtr.Zero);
}
finally
{
if (dialog != null)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(dialog);
}

所以如果我在 Show 方法调用之前添加对 SetOptions 方法的调用,我会得到一个异常:“尝试读取或写入 protected 内存。这通常表明其他内存已损坏。”

我试图在 .Net 2.0 甚至 4.0 上运行它。

这里有什么错误?为什么只调用 Show 方法有效,但如果我在它失败之前尝试其他方法?

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com