【发布时间】:2012-01-07 17:47:48
【问题描述】:
是否可以从我的 C# 应用程序中获取当前在 Windows 资源管理器中选择的文件列表?
我对通过 C# 等托管语言与 Windows 资源管理器进行交互的不同方法进行了大量研究。最初,我正在研究 shell 扩展的实现(例如 here 和 here),但显然在托管代码中这是一个坏主意,而且对于我的情况来说可能是矫枉过正。
接下来,我查看了 PInvoke/COM 解决方案,发现了 this article,这导致我找到了这段代码:
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 对象没有获取当前文件选择的方法,尽管它们可用于获取有关窗口的信息。
然后我发现this article 完全符合我的要求,但使用的是 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 解决方案?
【问题讨论】:
标签: c# .net windows com pinvoke