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);
}
下面是運行後的效果: