在Windows窗体应用程序中嵌入文件资源管理器实例,可以使用以下方法:
以下是使用WebBrowser控件的示例代码:
using System;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
webBrowser1.Navigate("explorer.exe");
}
}
}
以上代码将在WebBrowser控件中嵌入文件资源管理器实例。
使用第三方控件Explorer Browser的示例代码如下:
using System;
using System.Windows.Forms;
using ShellDll;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
explorerBrowser1.Navigate("C:\\");
}
}
}
以上代码将在Explorer Browser控件中嵌入文件资源管理器实例。
使用Windows API的示例代码如下:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
[DllImport("shell32.dll")]
static extern int SHCreateShellItem(IntPtr pidlParent, IntPtr psfParent, IntPtr pidl, out IShellItem ppsi);
public Form1()
{
InitializeComponent();
SHCreateShellItem(IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, out IShellItem ppsi);
ppsi.BindToHandler(null, BHID.BHID_SFObject, typeof(IShellFolder).GUID, out object folder);
IShellFolder2 shellFolder = (IShellFolder2)folder;
shellFolder.GetDefaultSearchGUID(out Guid category);
shellFolder.EnumObjects(IntPtr.Zero, SHCONTF.SHCONTF_FOLDERS | SHCONTF.SHCONTF_NONFOLDERS | SHCONTF.SHCONTF_INCLUDEHIDDEN, out IEnumIDList enumIDList);
enumIDList.Reset();
IntPtr[] pidls = new IntPtr[1];
uint fetched;
while (enumIDList.Next(1, pidls, out fetched) == 0)
{
SHCreateShellItem(IntPtr.Zero, IntPtr.Zero, pidls[0], out IShellItem item);
listView1.Items.Add(item.GetDisplayName(SIGDN.SIGDN_NORMALDISPLAY), item.GetDisplayName(SIGDN.SIGDN_PARENTRELATIVEPARSING));
}
}
}
}
以上代码将在ListView控件中嵌入文件资源管理器实例。
领取专属 10元无门槛券
手把手带您无忧上云