C# SHMultiFileProperties查看多个文件属性

[DllImport("shell32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SHMultiFileProperties(IDataObject pdtobj, int dwFlags);
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr ILCreateFromPath(string path);
[DllImport("shell32.dll", CharSet = CharSet.None)]
public static extern void ILFree(IntPtr pidl);
[DllImport("shell32.dll", CharSet = CharSet.None)]
public static extern int ILGetSize(IntPtr pidl);

private static MemoryStream CreateShellIDList(System.Collections.Specialized.StringCollection filenames)
{
    // first convert all files into pidls list
    int pos = 0;
    byte[][] pidls = new byte[filenames.Count][];
    foreach (Object filename in filenames)
    {
        // Get pidl based on name
        IntPtr pidl = ILCreateFromPath(filename.ToString());
        int pidlSize = ILGetSize(pidl);
        // Copy over to our managed array
        pidls[pos] = new byte[pidlSize];
        Marshal.Copy(pidl, pidls[pos++], 0, pidlSize);
        ILFree(pidl);
    }

    // Determine where in CIDL we will start pumping PIDLs
    int pidlOffset = 4 * (filenames.Count + 2);
    // Start the CIDL stream
    MemoryStream memStream = new MemoryStream();
    BinaryWriter sw = new BinaryWriter(memStream);
    // Initialize CIDL witha count of files
    sw.Write(filenames.Count);
    // Calcualte and write relative offsets of every pidl starting with root
    sw.Write(pidlOffset);
    pidlOffset += 4; // root is 4 bytes
    foreach (byte[] pidl in pidls)
    {
        sw.Write(pidlOffset);
        pidlOffset += pidl.Length;
    }

    // Write the root pidl (0) followed by all pidls
    sw.Write(0);
    foreach (byte[] pidl in pidls) sw.Write(pidl);
    // stream now contains the CIDL
    return memStream;
}

//调用:
string[] strArr = new string[] { @"C:\Lee", @"c:\windows" };
System.Collections.Specialized.StringCollection coll = new System.Collections.Specialized.StringCollection();
coll.AddRange(strArr);
DataObject data = new DataObject();
data.SetData("Preferred DropEffect", true, new MemoryStream(new byte[] { 5, 0, 0, 0 }));
data.SetData("Shell IDList Array", true, CreateShellIDList(coll));
data.SetFileDropList(coll);
SHMultiFileProperties(data, 0);

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值