从C#上的Windows shell上下文菜单中获取多个文件的方法如下:
using System.Runtime.InteropServices;
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
public static extern bool ShellExecuteEx(ref SHELLEXECUTEINFO lpExecInfo);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct SHELLEXECUTEINFO
{
public int cbSize;
public uint fMask;
public IntPtr hwnd;
public string lpVerb;
public string lpFile;
public string lpParameters;
public string lpDirectory;
public int nShow;
public IntPtr hInstApp;
public IntPtr lpIDList;
public string lpClass;
public IntPtr hkeyClass;
public uint dwHotKey;
public IntPtr hIcon;
public IntPtr hProcess;
}
private void GetSelectedFiles()
{
// 初始化SHELLEXECUTEINFO结构体
SHELLEXECUTEINFO info = new SHELLEXECUTEINFO();
info.cbSize = Marshal.SizeOf(info);
info.lpVerb = "open";
info.lpFile = "explorer.exe";
info.lpParameters = "/select, \"C:\\path\\to\\file.txt\"";
info.nShow = 0;
// 调用ShellExecuteEx函数
ShellExecuteEx(ref info);
}
这段代码将会打开一个文件选择器,并选中指定的文件。可以通过修改lpParameters参数来选中多个文件。
需要注意的是,这种方法只能在Windows操作系统上使用,并且需要管理员权限才能运行。
领取专属 10元无门槛券
手把手带您无忧上云