• <bdo id='ZAGr3'></bdo><ul id='ZAGr3'></ul>

    1. <i id='ZAGr3'><tr id='ZAGr3'><dt id='ZAGr3'><q id='ZAGr3'><span id='ZAGr3'><b id='ZAGr3'><form id='ZAGr3'><ins id='ZAGr3'></ins><ul id='ZAGr3'></ul><sub id='ZAGr3'></sub></form><legend id='ZAGr3'></legend><bdo id='ZAGr3'><pre id='ZAGr3'><center id='ZAGr3'></center></pre></bdo></b><th id='ZAGr3'></th></span></q></dt></tr></i><div id='ZAGr3'><tfoot id='ZAGr3'></tfoot><dl id='ZAGr3'><fieldset id='ZAGr3'></fieldset></dl></div>

      <tfoot id='ZAGr3'></tfoot>

      <small id='ZAGr3'></small><noframes id='ZAGr3'>

        <legend id='ZAGr3'><style id='ZAGr3'><dir id='ZAGr3'><q id='ZAGr3'></q></dir></style></legend>

        从 C# 应用程序获取 WindowsExplorer 中的当前选择?

        时间:2023-07-27

          <legend id='HbncM'><style id='HbncM'><dir id='HbncM'><q id='HbncM'></q></dir></style></legend>
        • <i id='HbncM'><tr id='HbncM'><dt id='HbncM'><q id='HbncM'><span id='HbncM'><b id='HbncM'><form id='HbncM'><ins id='HbncM'></ins><ul id='HbncM'></ul><sub id='HbncM'></sub></form><legend id='HbncM'></legend><bdo id='HbncM'><pre id='HbncM'><center id='HbncM'></center></pre></bdo></b><th id='HbncM'></th></span></q></dt></tr></i><div id='HbncM'><tfoot id='HbncM'></tfoot><dl id='HbncM'><fieldset id='HbncM'></fieldset></dl></div>
        • <tfoot id='HbncM'></tfoot>
            <tbody id='HbncM'></tbody>

              1. <small id='HbncM'></small><noframes id='HbncM'>

                  <bdo id='HbncM'></bdo><ul id='HbncM'></ul>
                  本文介绍了从 C# 应用程序获取 WindowsExplorer 中的当前选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  是否可以从我的 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 中的当前选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 Visual Studio 2012 中创建 HelloWorld COM 互操作 下一篇:我可以通过 COM 从 VBA 调用 C# 类的静态方法吗?

                  相关文章

                  <i id='REdYt'><tr id='REdYt'><dt id='REdYt'><q id='REdYt'><span id='REdYt'><b id='REdYt'><form id='REdYt'><ins id='REdYt'></ins><ul id='REdYt'></ul><sub id='REdYt'></sub></form><legend id='REdYt'></legend><bdo id='REdYt'><pre id='REdYt'><center id='REdYt'></center></pre></bdo></b><th id='REdYt'></th></span></q></dt></tr></i><div id='REdYt'><tfoot id='REdYt'></tfoot><dl id='REdYt'><fieldset id='REdYt'></fieldset></dl></div>

                  1. <tfoot id='REdYt'></tfoot>
                    <legend id='REdYt'><style id='REdYt'><dir id='REdYt'><q id='REdYt'></q></dir></style></legend>

                    <small id='REdYt'></small><noframes id='REdYt'>

                    • <bdo id='REdYt'></bdo><ul id='REdYt'></ul>