powershell shell32编程(文件属性表(对话框))

17 篇文章 0 订阅
5 篇文章 0 订阅
本文介绍了如何使用C#和C++中的Shell32.dll库,通过SHObjectProperties和SHMultiFileProperties函数处理单个和多个文件的属性,包括创建和操作ShellIDList以显示文件属性信息。
摘要由CSDN通过智能技术生成

声明:未经允许不得转载,转载时请标注来源;禁止用作商业及其他衍生目的。

有误请指正,侵权请联系删除。有更好的方法也请联系我

部分由c#,c++翻译来。

1.对于单个文件的属性表:SHObjectProperties

        Add-Type @"
using System;  
using System.Runtime.InteropServices;  
public class YZX
        {
[DllImport("shell32.dll", SetLastError=true)]
public static extern bool SHObjectProperties(int hwnd, int shopObjectType, [MarshalAs(UnmanagedType.LPWStr)] string fileName, [MarshalAs(UnmanagedType.LPWStr)] string PropertyPage);
}
"@ -Language CSharp

 [YZX]::SHObjectProperties(0, 2, "文件(夹)路径", "选项卡")

展示:

2.对于多个文件的属性:SHMultiFileProperties

    Add-Type @"
using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Text;  
using System.IO;  
using System.Windows.Forms;  
using System.Runtime.InteropServices;

public class YZX
        {
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
public 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);
public 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;
}
}
"@ -Language CSharp -ReferencedAssemblies System.Drawing, System.Data, System.Windows.Forms
    $i = New-Object System.Collections.Specialized.StringCollection
    (get-childitem -LiteralPath "c:\windows").FullName | foreach-object {$i.Add($_)} #用于添加所展示的文件,有其他书写方式。
    $data = new-object System.Windows.Forms.DataObject
    $data.SetData("Preferred DropEffect", $true, (new-object System.IO.MemoryStream))
    $data.SetData("Shell IDList Array", $true, [YZX]::CreateShellIDList($i));
    [YZX]::SHMultiFileProperties($data, 0)
展示:

  • 34
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值