[IT168 技术文档]PIDL亦有“绝对路径”与“相对路径”的概念。表示“相对路径”的PIDL(本文简称为“相对PIDL”)只有一个ITEMIDLIST结构的元素,用于标识相对于父文件夹的“路径”;表示“绝对路径”的PIDL(简称为“绝对PIDL”)有若干个ITEMIDLIST结构的元素,第一个元素表示外壳名字空间根文件夹(“桌面”)下的某一子文件夹A,第二个元素则表示文件夹A下的某一子文件夹B,其余依此类推。这样绝对PIDL就通过保存一条从“桌面”下的直接子文件夹或文件的绝对PIDL与相对PIDL是相同的,而其他的文件夹或文件的相对PIDL就只是其绝对PIDL的最后一部分了。
为什么要说这些呢?因为有些函数,必须使用绝对PIDL,例如图标,如果不使用绝对PIDL,某些图标是无法正常获得的(驱动器、控制面板等)。
但使用 EnumObjects 获得的,仅仅是相对PIDL,如果通过相对PIDL获取绝对PIDL呢?我参考了开源项目 C# FileBrowser 中的 PIDL 类
PIDL.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Runtime.InteropServices;
namespace WinShell
{
public class PIDL
{
private IntPtr pidl = IntPtr.Zero;
public PIDL(IntPtr pidl, bool clone)
{
if (clone)
this.pidl = ILClone(pidl);
else
this.pidl = pidl;
}
public PIDL(PIDL pidl, bool clone)
{
if (clone)
this.pidl = ILClone(pidl.Ptr);
else
this.pidl = pidl.Ptr;
}
public IntPtr Ptr
{
get { return pidl; }
}
public void Insert(IntPtr insertPidl)
{
IntPtr newPidl = ILCombine(insertPidl, pidl);
Marshal.FreeCoTaskMem(pidl);
pidl = newPidl;
}
public void Free()
{
if (pidl != IntPtr.Zero)
{
Marshal.FreeCoTaskMem(pidl);
pidl = IntPtr.Zero;
}
}
private static int ItemIDSize(IntPtr pidl)
{
if (!pidl.Equals(IntPtr.Zero))
{
byte[] buffer = new byte[2];
Marshal.Copy(pidl, buffer, 0, 2);
return buffer[1] * 256 + buffer[0];
}
else
return 0;
}
private static int ItemIDListSize(IntPtr pidl)
{
if (pidl.Equals(IntPtr.Zero))
return 0;
else
{
int size = ItemIDSize(pidl);
int nextSize = Marshal.ReadByte(pidl, size) + (Marshal.ReadByte(pidl, size + 1) * 256);
while (nextSize > 0)
{
size += nextSize;
nextSize = Marshal.ReadByte(pidl, size) + (Marshal.ReadByte(pidl, size + 1) * 256);
}
return size;
}
}
public static IntPtr ILClone(IntPtr pidl)
{
int size = ItemIDListSize(pidl);
byte[] bytes = new byte[size + 2];
Marshal.Copy(pidl, bytes, 0, size);
IntPtr newPidl = Marshal.AllocCoTaskMem(size + 2);
Marshal.Copy(bytes, 0, newPidl, size + 2);
return newPidl;
}
public static IntPtr ILCombine(IntPtr pidl1, IntPtr pidl2)
{
int size1 = ItemIDListSize(pidl1);
int size2 = ItemIDListSize(pidl2);
IntPtr newPidl = Marshal.AllocCoTaskMem(size1 + size2 + 2);
byte[] bytes = new byte[size1 + size2 + 2];
Marshal.Copy(pidl1, bytes, 0, size1);
Marshal.Copy(pidl2, bytes, size1, size2);
Marshal.Copy(bytes, 0, newPidl, bytes.Length);
return newPidl;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Runtime.InteropServices;
namespace WinShell
{
public class PIDL
{
private IntPtr pidl = IntPtr.Zero;
public PIDL(IntPtr pidl, bool clone)
{
if (clone)
this.pidl = ILClone(pidl);
else
this.pidl = pidl;
}
public PIDL(PIDL pidl, bool clone)
{
if (clone)
this.pidl = ILClone(pidl.Ptr);
else
this.pidl = pidl.Ptr;
}
public IntPtr Ptr
{
get { return pidl; }
}
public void Insert(IntPtr insertPidl)
{
IntPtr newPidl = ILCombine(insertPidl, pidl);
Marshal.FreeCoTaskMem(pidl);
pidl = newPidl;
}
public void Free()
{
if (pidl != IntPtr.Zero)
{
Marshal.FreeCoTaskMem(pidl);
pidl = IntPtr.Zero;
}
}
private static int ItemIDSize(IntPtr pidl)
{
if (!pidl.Equals(IntPtr.Zero))
{
byte[] buffer = new byte[2];
Marshal.Copy(pidl, buffer, 0, 2);
return buffer[1] * 256 + buffer[0];
}
else
return 0;
}
private static int ItemIDListSize(IntPtr pidl)
{
if (pidl.Equals(IntPtr.Zero))
return 0;
else
{
int size = ItemIDSize(pidl);
int nextSize = Marshal.ReadByte(pidl, size) + (Marshal.ReadByte(pidl, size + 1) * 256);
while (nextSize > 0)
{
size += nextSize;
nextSize = Marshal.ReadByte(pidl, size) + (Marshal.ReadByte(pidl, size + 1) * 256);
}
return size;
}
}
public static IntPtr ILClone(IntPtr pidl)
{
int size = ItemIDListSize(pidl);
byte[] bytes = new byte[size + 2];
Marshal.Copy(pidl, bytes, 0, size);
IntPtr newPidl = Marshal.AllocCoTaskMem(size + 2);
Marshal.Copy(bytes, 0, newPidl, size + 2);
return newPidl;
}
public static IntPtr ILCombine(IntPtr pidl1, IntPtr pidl2)
{
int size1 = ItemIDListSize(pidl1);
int size2 = ItemIDListSize(pidl2);
IntPtr newPidl = Marshal.AllocCoTaskMem(size1 + size2 + 2);
byte[] bytes = new byte[size1 + size2 + 2];
Marshal.Copy(pidl1, bytes, 0, size1);
Marshal.Copy(pidl2, bytes, size1, size2);
Marshal.Copy(bytes, 0, newPidl, bytes.Length);
return newPidl;
}
}
}
该类实现了 PIDL 的复制和结合功能。现在我们修改 ShellItem 类,使它带有父节点的 IShellFolder 以及提供获取绝对 PIDL 的属性:
private ShellItem m_ParentItem;
public ShellItem ParentItem
{
get { return m_ParentItem; }
set { m_ParentItem = value; }
}
/**//// <summary>
/// 绝对 PIDL
/// </summary>
public PIDL PIDLFull
{
get
{
PIDL pidlFull = new PIDL(PIDL, true);
ShellItem current = ParentItem;
while (current != null)
{
pidlFull.Insert(current.PIDL);
current = current.ParentItem;
}
return pidlFull;
}
}
public ShellItem ParentItem
{
get { return m_ParentItem; }
set { m_ParentItem = value; }
}
/**//// <summary>
/// 绝对 PIDL
/// </summary>
public PIDL PIDLFull
{
get
{
PIDL pidlFull = new PIDL(PIDL, true);
ShellItem current = ParentItem;
while (current != null)
{
pidlFull.Insert(current.PIDL);
current = current.ParentItem;
}
return pidlFull;
}
}