声明:未经允许不得转载,转载时请标注来源;禁止用作商业及其他衍生目的。
有误请指正,侵权请联系删除。有更好的方法也请联系我
部分由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)
展示: