windows 从 C# 应用程序获取 WindowsExplorer 中的当前选择?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8292953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get current selection in WindowsExplorer from a C# application?
提问by WilHall
Is it possible to get a list of the files that are currently selected in Windows Explorer from my C# app?
是否可以从我的 C# 应用程序中获取当前在 Windows 资源管理器中选择的文件列表?
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 (hereand herefor example), but apparently that is a bad idea from within managed code, and is probably overkill for my situation anyway.
我对使用 C# 等托管语言与 Windows 资源管理器进行交互的不同方法进行了大量研究。最初,我正在研究 shell 扩展的实现(例如here和here),但显然这在托管代码中是一个坏主意,无论如何对于我的情况可能是过度的。
Next, I looked into PInvoke/COM solutions, and found this article, which led me to this code:
接下来,我查看了 PInvoke/COM 解决方案,并找到了这篇文章,这使我找到了以下代码:
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);
}
}
}
...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.
...但是单个InternetExplorer
对象没有获取当前文件选择的方法,尽管它们可用于获取有关窗口的信息。
Then I found this articledoing 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:
然后我发现这篇文章完全符合我的需要,但使用的是 C++。以此为起点,我尝试通过shell32.dll
在我的项目中添加作为参考来进行一些翻译。我最终得到了以下结果:
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)
{
...
}
}
}
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.
这稍微接近了一点,因为我能够Folder
为窗口和每个项目获取一个对象,但我仍然看不到获取当前选择的方法。
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?
我可能完全看错了地方,但我一直在遵循我所拥有的唯一线索。谁能指出我合适的 PInvoke/COM 解决方案?
采纳答案by WilHall
Finally figured out a solution, thanks to this question: Get selected items of folder with WinAPI.
终于找到了一个解决方案,感谢这个问题: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);
}
}
}
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.
显然window.Document
对应于资源管理器窗口内的实际文件夹视图,这不是很直观。但除了误导性的变量/方法名称之外,这非常有效。