windows shell 編程2(瀏覽檔案夾)

來源:互聯網
上載者:User

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);
}

 


下面是運行後的效果:


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.