是否可以从我的 C# 应用程序中获取当前在 Windows 资源管理器中选择的文件列表?
Is it possible to get a list of the files that are currently selected in Windows Explorer from my C# app?
我对通过 C# 等托管语言与 Windows 资源管理器进行交互的不同方法进行了大量研究.最初,我正在查看 shell 扩展的实现(这里和 这里 例如),但显然这是一个坏主意代码,无论如何对于我的情况来说可能是矫枉过正.
I have done a lot of research on different methods of interacting with Windows Explorer from a managed language like C#. Initially, I was looking at implementations of shell extensions (here and here for example), but apparently that is a bad idea from within managed code, and is probably overkill for my situation anyway.
接下来,我研究了 PInvoke/COM 解决方案,发现 这篇文章,这导致了我到此代码:
Next, I looked into PInvoke/COM solutions, and found this article, which led me to this code:
SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();
string filename;
ArrayList windows = new ArrayList();
foreach(SHDocVw.InternetExplorer ie in shellWindows)
{
filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
if(filename.Equals("explorer"))
{
Console.WriteLine("Hard Drive: {0}", ie.LocationURL);
windows.Add(ie);
var shell = new Shell32.Shell();
foreach (SHDocVw.InternetExplorerMedium sw in shell.Windows())
{
Console.WriteLine(sw.LocationURL);
}
}
}
...但是各个 InternetExplorer
对象没有获取当前文件选择的方法,尽管它们可用于获取有关窗口的信息.
...But the individual InternetExplorer
objects have no methods to get the current file selection, though they can be used to get information about the window.
然后我找到了这篇文章完全按照我的需要做,但是在 C++ 中.以此为起点,我尝试通过在我的项目中添加 shell32.dll
作为参考来进行一些翻译.我最终得到以下结果:
Then I found this article doing exactly what I needed, but in C++. Using this as a starting point, I attempted to do some translation by adding shell32.dll
as a reference in my project. I ended up with the following:
SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();
string filename;
ArrayList windows = new ArrayList();
foreach(SHDocVw.InternetExplorer ie in shellWindows)
{
filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
if(filename.Equals("explorer"))
{
Console.WriteLine("Hard Drive: {0}", ie.LocationURL);
windows.Add(ie);
var shell = (Shell32.IShellDispatch4)new Shell32.Shell();
Shell32.Folder folder = shell.NameSpace(ie.LocationURL);
Shell32.FolderItems items = folder.Items();
foreach (Shell32.FolderItem item in items)
{
...
}
}
}
这稍微接近了一点,因为我能够为窗口和每个项目获取一个 Folder
对象,但我仍然看不到获取当前选择的方法.
This was slightly closer, because I am able to get a Folder
object for the window, and for each item, but I still do not see a way to get the current selection.
我可能完全看错了地方,但我一直在跟踪我仅有的线索.谁能给我指出合适的 PInvoke/COM 解决方案?
I may be looking entirely in the wrong place, but I've been following the only leads I have. Can anyone point me to the appropriate PInvoke/COM solution?
终于想出了一个解决方案,感谢这个问题:使用 WinAPI 获取选定的文件夹项.
Finally figured out a solution, thanks to this question: Get selected items of folder with WinAPI.
我最终得到了以下内容,以获取当前选定文件的列表:
I ended up with the following, in order to get a list of currently selected files:
IntPtr handle = GetForegroundWindow();
List<string> selected = new List<string>();
var shell = new Shell32.Shell();
foreach(SHDocVw.InternetExplorer window in shell.Windows())
{
if (window.HWND == (int)handle)
{
Shell32.FolderItems items = ((Shell32.IShellFolderViewDual2)window.Document).SelectedItems();
foreach(Shell32.FolderItem item in items)
{
selected.Add(item.Path);
}
}
}
显然 window.Document
对应于资源管理器窗口中的实际文件夹视图,这不是很直观.但除了具有误导性的变量/方法名称之外,这非常有效.
Apparently window.Document
corresponds to the actual folder view inside the explorer window, which isn't very intuitive. But other than the misleading variable/method names, this works perfectly.
这篇关于从 C# 应用程序获取 WindowsExplorer 中的当前选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!