title: Get the selected files in Open File Dialog
body: <p>I need to get the selected files in a OpenFileDialog. For a single file, I'm doing this:</p>
  
  <p>EDIT:</p>
  
  <pre><code>var count = SelectedItems(handle); // handle is the handle to ListView control in OpenFiledialog
  var bufferSize = 2048 * count;
  if (bufferSize &gt; 0)
  {
      var path = new StringBuilder(bufferSize)
      SendMessage(handle, CDM_GETFILEPATH, (IntPtr)path.Capacity, path);
  }
  </code></pre>
  
  <p>And it works fine, but I need to check for multiple files (Multiselect = true). I don't know the buffer size (stringBuilder size). </p>
  
  <p>EDIT: I'm customizing the Open File Dialog. I'm trying to get the selected files before the OFD window is closed.</p>
  
  <p>EDIT: Currently I can have a list of selected files, BUT, only the filenames, without path, I'm using this:</p>
  
  <pre><code>var totalItemsCount = SendMessage(handle, LVM_GETITEMCOUNT, 0, 0);
  var fileNames = new List&lt;string&gt;();
  var lvi = new LVITEM();
  var lviPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(LVITEM)));
  
  for (var i = 0; i &lt; totalItemsCount; i++)
  {
      var pcsText = Marshal.AllocHGlobal(1024);
  
      lvi.iSubItem = 0;
      lvi.cchTextMax = 1024;
      lvi.pszText = pcsText;
  
      Marshal.StructureToPtr(lvi, lviPtr, fDeleteOld: true);
  
      var success = SendMessage(handle, LVM_GETITEMTEXT, i, (int)lviPtr);
      if (success &gt; 0)
      {
          var itemState = SendMessage(handle, LVM_GETITEMSTATE, i, 2);
          var selected = (itemState &amp; 2) != 0;
          if (selected = (itemState &amp; 2) != 0)
          {
              lvi = (LVITEM)Marshal.PtrToStructure(lviPtr, typeof(LVITEM));
              var name = Marshal.PtrToStringAnsi(lvi.pszText);
              fileNames.Add(name);
          }
       }
  
       Marshal.FreeHGlobal(pcsText);
   }
  
   Marshal.FreeHGlobal(lviPtr);
  </code></pre>
  
  <p>What I'm doing is to get the selected files directly from the listview, but I need gthe filenames with the path. </p>
  
  <p>When the OpenfileDialog change the folder, I can get the folder path with:</p>
  
  <pre><code>var folderPath = new StringBuilder(256);
  SendMessage(handle, CDM_GETFOLDERPATH, (IntPtr)256, folderPath);
  </code></pre>
  
  <p>But it doesnt work when the folder is an special folder, like the libraries.</p>