从 C# 应用程序获取 WindowsExplorer 中的当前选择?
是否可以从我的 C# 应用程序获取当前在 Windows 资源管理器中选择的文件列表?
我对通过 C# 等托管语言与 Windows 资源管理器交互的不同方法进行了大量研究。最初,我正在研究 shell 扩展的实现(此处和此处为例),但显然这是一个托管代码中的想法很糟糕,而且对于我的情况来说可能有点过分了。
接下来,我研究了 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);
}
}
}
...但是各个 InternetExplorer
对象没有方法来获取当前文件选择,尽管它们可用于获取有关窗口的信息。
然后我发现这篇文章所做的正是我需要,但是是用 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)
{
...
}
}
}
这稍微接近一点,因为我能够为窗口和每个项目获取一个 Folder
对象,但我仍然没有找到获取当前选择的方法。
我可能完全找错了地方,但我一直在遵循我所掌握的唯一线索。谁能给我指出合适的 PInvoke/COM 解决方案?
Is it possible to get a list of the files that are currently selected in Windows Explorer from my C# app?
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.
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);
}
}
}
...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.
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)
{
...
}
}
}
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
终于找到了一个解决方案,感谢这个问题: 获取文件夹的选定项目WinAPI。
为了获取当前选定文件的列表,我最终得到了以下结果:
显然
window.Document
对应于资源管理器窗口内的实际文件夹视图,这不是很直观。但除了误导性的变量/方法名称之外,这一切都很完美。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:
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.