使用 WinAPI 获取文件夹的选定项目
我尝试获取用户正在使用的文件夹的选定文件。我有以下代码已经在运行,但仅在桌面文件上:
private string selectedFiles()
{
// get the handle of the desktop listview
IntPtr vHandle = WinApiWrapper.FindWindow("Progman", "Program Manager");
vHandle = WinApiWrapper.FindWindowEx(vHandle, IntPtr.Zero, "SHELLDLL_DefView", null);
vHandle = WinApiWrapper.FindWindowEx(vHandle, IntPtr.Zero, "SysListView32", "FolderView");
//IntPtr vHandle = WinApiWrapper.GetForegroundWindow();
//Get total count of the icons on the desktop
int vItemCount = WinApiWrapper.SendMessage(vHandle, WinApiWrapper.LVM_GETITEMCOUNT, 0, 0);
//MessageBox.Show(vItemCount.ToString());
uint vProcessId;
WinApiWrapper.GetWindowThreadProcessId(vHandle, out vProcessId);
IntPtr vProcess = WinApiWrapper.OpenProcess(WinApiWrapper.PROCESS_VM_OPERATION | WinApiWrapper.PROCESS_VM_READ |
WinApiWrapper.PROCESS_VM_WRITE, false, vProcessId);
IntPtr vPointer = WinApiWrapper.VirtualAllocEx(vProcess, IntPtr.Zero, 4096,
WinApiWrapper.MEM_RESERVE | WinApiWrapper.MEM_COMMIT, WinApiWrapper.PAGE_READWRITE);
try
{
for (int j = 0; j < vItemCount; j++)
{
byte[] vBuffer = new byte[256];
WinApiWrapper.LVITEM[] vItem = new WinApiWrapper.LVITEM[1];
vItem[0].mask = WinApiWrapper.LVIF_TEXT;
vItem[0].iItem = j;
vItem[0].iSubItem = 0;
vItem[0].cchTextMax = vBuffer.Length;
vItem[0].pszText = (IntPtr)((int)vPointer + Marshal.SizeOf(typeof(WinApiWrapper.LVITEM)));
uint vNumberOfBytesRead = 0;
WinApiWrapper.WriteProcessMemory(vProcess, vPointer,
Marshal.UnsafeAddrOfPinnedArrayElement(vItem, 0),
Marshal.SizeOf(typeof(WinApiWrapper.LVITEM)), ref vNumberOfBytesRead);
WinApiWrapper.SendMessage(vHandle, WinApiWrapper.LVM_GETITEMW, j, vPointer.ToInt32());
WinApiWrapper.ReadProcessMemory(vProcess,
(IntPtr)((int)vPointer + Marshal.SizeOf(typeof(WinApiWrapper.LVITEM))),
Marshal.UnsafeAddrOfPinnedArrayElement(vBuffer, 0),
vBuffer.Length, ref vNumberOfBytesRead);
string vText = Encoding.Unicode.GetString(vBuffer, 0,
(int)vNumberOfBytesRead);
string IconName = vText;
//Check if item is selected
var result = WinApiWrapper.SendMessage(vHandle, WinApiWrapper.LVM_GETITEMSTATE, j, (int)WinApiWrapper.LVIS_SELECTED);
if (result == WinApiWrapper.LVIS_SELECTED)
{
return vText;
}
}
}
finally
{
WinApiWrapper.VirtualFreeEx(vProcess, vPointer, 0, WinApiWrapper.MEM_RELEASE);
WinApiWrapper.CloseHandle(vProcess);
}
return String.Empty;
}
我尝试使用 GetForegroundWindow() 获取窗口句柄,然后调用 SHELLDLL_DefView 但没有成功。
那么如何更改前 3 行以获得当前使用的文件夹的句柄?
I try to get the selected files of a folder which the user is using. I have the following code which is already running, but only on desktop files:
private string selectedFiles()
{
// get the handle of the desktop listview
IntPtr vHandle = WinApiWrapper.FindWindow("Progman", "Program Manager");
vHandle = WinApiWrapper.FindWindowEx(vHandle, IntPtr.Zero, "SHELLDLL_DefView", null);
vHandle = WinApiWrapper.FindWindowEx(vHandle, IntPtr.Zero, "SysListView32", "FolderView");
//IntPtr vHandle = WinApiWrapper.GetForegroundWindow();
//Get total count of the icons on the desktop
int vItemCount = WinApiWrapper.SendMessage(vHandle, WinApiWrapper.LVM_GETITEMCOUNT, 0, 0);
//MessageBox.Show(vItemCount.ToString());
uint vProcessId;
WinApiWrapper.GetWindowThreadProcessId(vHandle, out vProcessId);
IntPtr vProcess = WinApiWrapper.OpenProcess(WinApiWrapper.PROCESS_VM_OPERATION | WinApiWrapper.PROCESS_VM_READ |
WinApiWrapper.PROCESS_VM_WRITE, false, vProcessId);
IntPtr vPointer = WinApiWrapper.VirtualAllocEx(vProcess, IntPtr.Zero, 4096,
WinApiWrapper.MEM_RESERVE | WinApiWrapper.MEM_COMMIT, WinApiWrapper.PAGE_READWRITE);
try
{
for (int j = 0; j < vItemCount; j++)
{
byte[] vBuffer = new byte[256];
WinApiWrapper.LVITEM[] vItem = new WinApiWrapper.LVITEM[1];
vItem[0].mask = WinApiWrapper.LVIF_TEXT;
vItem[0].iItem = j;
vItem[0].iSubItem = 0;
vItem[0].cchTextMax = vBuffer.Length;
vItem[0].pszText = (IntPtr)((int)vPointer + Marshal.SizeOf(typeof(WinApiWrapper.LVITEM)));
uint vNumberOfBytesRead = 0;
WinApiWrapper.WriteProcessMemory(vProcess, vPointer,
Marshal.UnsafeAddrOfPinnedArrayElement(vItem, 0),
Marshal.SizeOf(typeof(WinApiWrapper.LVITEM)), ref vNumberOfBytesRead);
WinApiWrapper.SendMessage(vHandle, WinApiWrapper.LVM_GETITEMW, j, vPointer.ToInt32());
WinApiWrapper.ReadProcessMemory(vProcess,
(IntPtr)((int)vPointer + Marshal.SizeOf(typeof(WinApiWrapper.LVITEM))),
Marshal.UnsafeAddrOfPinnedArrayElement(vBuffer, 0),
vBuffer.Length, ref vNumberOfBytesRead);
string vText = Encoding.Unicode.GetString(vBuffer, 0,
(int)vNumberOfBytesRead);
string IconName = vText;
//Check if item is selected
var result = WinApiWrapper.SendMessage(vHandle, WinApiWrapper.LVM_GETITEMSTATE, j, (int)WinApiWrapper.LVIS_SELECTED);
if (result == WinApiWrapper.LVIS_SELECTED)
{
return vText;
}
}
}
finally
{
WinApiWrapper.VirtualFreeEx(vProcess, vPointer, 0, WinApiWrapper.MEM_RELEASE);
WinApiWrapper.CloseHandle(vProcess);
}
return String.Empty;
}
I tried to get the window handle with GetForegroundWindow() and then call the SHELLDLL_DefView without success.
So how can I change the first 3 rows to get me the handle of the current folder in use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要完成各种 shell 对象和接口明确支持的事情需要进行大量的黑客工作。虽然文档并不容易发现,但功能是存在的。 Raymond Chen 写了一篇关于使用这些界面的精彩文章。似乎没有办法获取“当前”文件夹,但我想您可以获取 HWND 并查看是否有前台窗口。
That's a lot of hacking to do something that is explicitly supported by the various shell objects and interfaces. Granted the documentation doesn't make it easily discoverable, but the functionality is there. Raymond Chen wrote a great article about using these interfaces. There doesn't appear to be a way to get the "current" folder, though I guess you could get the HWNDs and see if any is the foreground window.
非常感谢。你给了我正确的方向。可以获取文件夹中选定的文件:
thank you very much. You gave me the right direction. It is possible to get the selected files of a folder: