title: Get current selection in WindowsExplorer from a C# application?
body: <p>Is it possible to get a list of the files that are currently selected in Windows Explorer from my C# app?</p>
  
  <p>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 (<a href="http://www.theserverside.net/tt/articles/showarticle.tss?id=ShellExtensions" rel="nofollow noreferrer">here</a> and <a href="https://stackoverflow.com/questions/2194572/windows-shell-extension-with-c-sharp">here</a> for example), but apparently that is a bad idea from within managed code, and is probably overkill for my situation anyway.</p>
  
  <p>Next, I looked into PInvoke/COM solutions, and found <a href="http://omegacoder.com/?p=63" rel="nofollow noreferrer">this article</a>, which led me to this code:</p>
  
  <pre><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);
                  }
  
              }
          }
  </code></pre>
  
  <p>...But the individual <code>InternetExplorer</code> objects have no methods to get the current file selection, though they can be used to get information about the window.</p>
  
  <p>Then I found <a href="http://blogs.msdn.com/b/oldnewthing/archive/2004/07/20/188696.aspx" rel="nofollow noreferrer">this article</a> doing exactly what I needed, but in C++. Using this as a starting point, I attempted to do some translation by adding <code>shell32.dll</code> as a reference in my project. I ended up with the following:</p>
  
  <pre><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 = (Shell32.IShellDispatch4)new Shell32.Shell();
                  Shell32.Folder folder = shell.NameSpace(ie.LocationURL);
                  Shell32.FolderItems items = folder.Items();
                  foreach (Shell32.FolderItem item in items)
                  {
                      ...
                  }
              }
          }
  </code></pre>
  
  <p>This was slightly closer, because I am able to get a <code>Folder</code> object for the window, and for each item, but I still do not see a way to get the current selection.</p>
  
  <p>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?</p>