windows shell 编程2(浏览文件夹)

Windows Shell 编程,即 Windows 外壳编程。我们所看到的资源管理器以及整个桌面,都是一个 Shell。

我们知道,在win32中是以外壳名字空间的形式来组织文件系统的,在外壳名字空间里的每一个对象(注)都实现了一个IShellFolder的接口,通过这个接口我们可以直接查询或间接得到其他相关的接口。

下面做个程序用于浏览文件夹:

在C#中,我们这样定义 IShellFolder 接口:

 

代码
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid(
"000214E6-0000-0000-C000-000000000046")]
public interface IShellFolder {
void ParseDisplayName(
IntPtr hwnd,
IntPtr pbc,
[MarshalAs(UnmanagedType.LPWStr)]
string pszDisplayName,
out uint pchEaten,
out IntPtr ppidl,
ref uint pdwAttributes);

[PreserveSig]
int EnumObjects(IntPtr hWnd, SHCONTF flags, out IntPtr enumIDList);

void BindToObject(
IntPtr pidl,
IntPtr pbc,
[In()]
ref Guid riid,
out IShellFolder ppv);

void BindToStorage(
IntPtr pidl,
IntPtr pbc,
[In()]
ref Guid riid,
[MarshalAs(UnmanagedType.Interface)]
out object ppv);

[PreserveSig()]
uint CompareIDs(
int lParam,
IntPtr pidl1,
IntPtr pidl2);

void CreateViewObject(
IntPtr hwndOwner,
[In()]
ref Guid riid,
[MarshalAs(UnmanagedType.Interface)]
out object ppv);

void GetAttributesOf(
uint cidl,
[In(), MarshalAs(UnmanagedType.LPArray)] IntPtr[] apidl,
ref SFGAO rgfInOut);
IntPtr GetUIObjectOf(
IntPtr hwndOwner,
uint cidl,
[MarshalAs(UnmanagedType.LPArray)] IntPtr[] apidl,
[In()]
ref Guid riid,
out IntPtr rgfReserved);

void GetDisplayNameOf(
IntPtr pidl,
SHGNO uFlags,
IntPtr lpName);

IntPtr SetNameOf(
IntPtr hwnd,
IntPtr pidl,
[MarshalAs(UnmanagedType.LPWStr)]
string pszName,
SHGNO uFlags);
}


桌面是最顶级的文件夹,外壳名字空间中其他各项都可以用从桌面开始的 PIDL 加以表示。

 可以通过 API  SHGetDesktopFolder获取“桌面”的 PIDL 和其 IShellFolder 接口

代码
public static IShellFolder GetDesktopFolder(out IntPtr ppshf) {
SHGetDesktopFolder(
out ppshf);
Object obj
= Marshal.GetObjectForIUnknown(ppshf);
return (IShellFolder)obj;
}

 

下面是程序调用的代码:

代码
private void Form1_Load(object sender, EventArgs e){

//获得桌面 PIDL
IntPtr desktopPtr;
IShellFolder desktop
= API.GetDesktopFolder(out desktopPtr);

//获取 C 盘的 PIDL
string FolderPath = @"C:\";
IntPtr Pidl
= IntPtr.Zero;
IShellFolder Root;
uint i, j = 0;
desktop.ParseDisplayName(Handle, IntPtr.Zero, FolderPath,
out i, out Pidl, ref j);
desktop.BindToObject(Pidl, IntPtr.Zero,
ref Guids.IID_IShellFolder, out Root);
Marshal.ReleaseComObject(desktop);

//循环查找 C 盘下面的文件/文件夹的 PIDL
IEnumIDList fileEnum = null;
IEnumIDList folderEnum
= null;
IntPtr fileEnumPtr
= IntPtr.Zero;
IntPtr folderEnumPtr
= IntPtr.Zero;
IntPtr pidlSub;
int celtFetched;

//获取子文件夹
if (Root.EnumObjects(this.Handle, SHCONTF.FOLDERS | SHCONTF.INCLUDEHIDDEN, out fileEnumPtr) == API.S_OK)
{
fileEnum
= (IEnumIDList)Marshal.GetObjectForIUnknown(fileEnumPtr);
while (fileEnum.Next(1, out pidlSub, out celtFetched) == 0 && celtFetched == API.S_FALSE)
{
//获取显示名称
string name = API.GetNameByPIDL(pidlSub);
lvFile.Items.Add(name,
1);
}
}

//获取子文件
if (Root.EnumObjects(this.Handle, SHCONTF.NONFOLDERS | SHCONTF.INCLUDEHIDDEN, out folderEnumPtr) == API.S_OK)
{
folderEnum
= (IEnumIDList)Marshal.GetObjectForIUnknown(folderEnumPtr);
while (folderEnum.Next(1, out pidlSub, out celtFetched) == 0 && celtFetched == API.S_FALSE)
{
string name = API.GetNameByPIDL(pidlSub);
lvFile.Items.Add(name,
0);
}
}

Marshal.ReleaseComObject(Root);
}

 


下面是运行后的效果:


posted on 2010-06-27 16:49  cjfwu  阅读(920)  评论(3编辑  收藏  举报

导航