title: How to get Windows Explorer's selected files from within C#?
body: <p>I need to get the current collection of files that are selected in Windows Explorer.  I found the following code from <a href="https://stackoverflow.com/questions/8292953/get-current-selection-in-windowsexplorer-from-a-c-sharp-application" title="here">here</a>.</p>
  
  <p>I'm not quite there, though.  For one thing, where does <code>GetForegroundWindow</code> come from?  And for another thing, the compiler complains on the line</p>
  
  <pre><code>var shell = new Shell32.Shell();
  </code></pre>
  
  <p>saying </p>
  
  <blockquote>
    <p>"The type or namespace name 'Shell32' could not be found (are you
    missing a using directive or an assembly reference?)".  I have added
    SHDocVw as a reference, but I still can't get past the compiler.  Can
    someone please help me get this completed?</p>
  </blockquote>
  
  <pre><code>    IntPtr handle = GetForegroundWindow();
  
      ArrayList selected = new ArrayList();
      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);
              }
          }
      }
  </code></pre>