title: C# OpenFileDialog open zip folder containing single file?
body: <p>I have an application which currently provides the user with the ability to view a PDF file inside the application by using File-&gt;Open, browsing to the location of the PDF file using a <code>Microsoft.Win32.OpenFileDialog</code>, and then displaying that PDF file in a <code>System.Windows.Controls.WebBrowser</code> in the GUI.</p>
  <p>I am creating the <code>OpenFileDialog</code> and setting the file extensions it can open using:</p>
  <pre><code>/*Create Open File dialog */
          Microsoft.Win32.OpenFileDialog OFDlg = new Microsoft.Win32.OpenFileDialog();
  
          /*Set filter for file extension and default file extension */
          OFDlg.DefaultExt = &quot;.pdf&quot;;
          OFDlg.Filter = &quot;PDF Documents (.pdf)|*.pdf&quot;;
  </code></pre>
  <p>I now want to extend this, so that the user can open a ZIP folder containing a single PDF document, and display that PDF document in the same way that I am above.</p>
  <p>I tried changing the filter to allow .zip files, i.e.</p>
  <pre><code>OFDlg.DefaultExt = &quot;.pdf|.zip&quot;;
  OFDlg.Filter = &quot;PDF Documents (.pdf)|*.pdf|ZIP|*.zip&quot;;
  </code></pre>
  <p>but when I browse to the location of the .zip file in the <code>OpenFileDialog</code>, the .zip folder is not displayed there- only normal folders and PDF documents (other documents in that directory, such as .doc &amp; .xls are not displayed in the <code>OpenFileDialog</code>).</p>
  <p>My reason for wanting to be able to open the contents of a .zip file directly from the .zip, rather than navigating to that file itself, is so that I can add public/private key encryption to the .zip, so that its contents can only be read securely.</p>
  <p>I know that there could in theory be problems if the .zip contains more than one file, but I intend to send each encrypted file in its own zip folder, so it can be assumed that any zip file that the user is trying to open contains a single .pdf, and nothing else.</p>
  <p>So my questions are:</p>
  <ol>
  <li>How can I make .zip folders visible from the <code>OpenFileDialog</code>?</li>
  <li>How can I make the selection of that .zip folder automatically open and display its contents (a single PDF file) in the <code>System.Window.Controls.WebBrowser</code> that I am currently using to display PDFs in my GUI?</li>
  </ol>
  <p><strong>Edit 1</strong></p>
  <p>I tried changing  my <code>OpenFile()</code> method to the following code:</p>
  <pre><code>/*Set filter for file extension and default file extension */
          OFDlg.DefaultExt = &quot;.pdf&quot;;
          OFDlg.DefaultExt = &quot;.zip&quot;;
          OFDlg.Filter = &quot;PDF Documents (.pdf)|*.pdf&quot;;
          OFDlg.Filter = &quot;ZIP Folders (.ZIP)|*.zip&quot;;
  </code></pre>
  <p>but when I now run my application, and browse to the same location, although the .zip folder is now shown in the <code>OpenFileDialog</code>, the .pdf files no longer are... and if I double click the .zip folder, my application breaks, and I get a runtime error on the line</p>
  <pre><code>PdfPanel.OpenFile(docFP);
  </code></pre>
  <p>which says:</p>
  <blockquote>
  <p>An unhandled exception of type 'System.AccessViolationException' occurred in MoonPdfLib.dll</p>
  <p>Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.</p>
  </blockquote>
  <p>I guess that's to do with the MoonPDF library that I'm using to read the PDFs being unable to handle the .zip extension?</p>
  <p>How would I resolve this to be able to open the PDF inside the ZIP?</p>
  <p><strong>Edit 2</strong></p>
  <p>Ok, so I've resolved the issue about only being able to see either the PDF files or the .ZIP folders (not both at the same time), by moving the functionality into two separate methods- one to display the PDF direct from the PDF's filepath, and another to display the PDF from the path of the .ZIP folder holding it.</p>
  <p>The method for displaying the PDFs directly currently works (it is essentially the code in the first bit of code I've quoted). However the method for displaying the PDFs from the ZIP currently doesn't work...</p>
  <p>I understand the reason for this- it's because I am passing a <code>.zip</code> folder to the <code>OpenFile</code> method... The code for this method currently looks like this:</p>
  <pre><code>private void openZipMenuItem_click(object sender, RoutedEventArgs e)
      {
          Microsoft.Win32.OpenFileDialog OZipDlg = new Microsoft.Win32.OpenFileDialog();
  
          OZipDlg.DefaultExt = &quot;.zip&quot;;
          OZipDlg.Filter = &quot;ZIP Folder (.zip)|*.zip&quot;;
  
          Nullable&lt;bool&gt; result = OZipDlg.ShowDialog();
  
          if (result == true)
          {
              /*Open document */
              string filename = OZipDlg.FileName;
              //fnTextBox.Text = filename;
              zipFP = OZipDlg.FileName;
              /*browser.Navigate(docFP); ERF (27/05/2016 @ 0935) Comment this line- I want to use PdfPanel to open docFP, not browser */
  
              Console.WriteLine(&quot;Panel height: &quot; + PdfPanel.ActualHeight);
              PdfPanel.OpenFile(zipFP);
  
          }
      }
  </code></pre>
  <p>When I try to call this function to open a .zip, I get a runtime exception which says:</p>
  <blockquote>
  <p>AccessViolationException was unhandled</p>
  <p>An unhandled exception of type 'System.AccessViolationException' occurred in MoonPdfLib.dll</p>
  <p>Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.</p>
  </blockquote>
  <p>I understand that I can't display a Zip folder in the <code>PdfPanel</code> (which is a <code>MoonPdfPanel</code> that I am using from the <code>MoonPdfLibrary</code>), so I know that I will get an exception here.</p>
  <p>How can I pass the contents of <code>zipFP</code> to the call to <code>PdfPanel.OpenFile()</code>, rather than passing <code>zipFP</code> itself to it?</p>
  <p><strong>Edit 3</strong></p>
  <p>Ok, so my code is currently extracting the PDF file successfully from the ZIP folder when I open it- I can see that it is copied to the directory I have specified. I am now trying to get the PDF to be displayed automatically in the PDF Panel on my application- I've done this by adding the following code:</p>
  <pre><code>try{
      string extractPath = @&quot;C:\Documents&quot;;
      using(ZipArchivev zip = ZipFile.Open(zipFP, ZipArchiveMode.Read))
          foreach(ZipArchiveEntry entry in zip.Entries){
              try{
                  ZipFile.ExtractToDirectory(zipFP, extractPath);
                  Console.WriteLine(&quot;zipFP: &quot; + zipFP);
              }catch(System.IOException){
                  Console.WriteLine(&quot;File already exists...&quot;);
              }
          }
  
          string ExtractedPDF = string.Concat(extractPath, zipFP);
          PdfPanel.OpenFile(ExtractedPDF);
  }catch(AccessViolationException ex){
      Console.WriteLine(&quot;Can't display a zip in the PDF panel...&quot; + ex.InnerException);
  }
  </code></pre>
  <p>But when my code tries to execute the line <code>PdfPanel.OpenFile(ExtracedPDF);</code>, I get an exception that says:</p>
  <blockquote>
  <p>FileNotFoundException was unhandled | An unhandled exception of type 'System.IO.FileNotFoundException' occurred in MoonPdfLib.dll'</p>
  </blockquote>
  <p>I understand that this is happening because the variable I am trying to display in the PDFPanel, <code>ExtractedPDF</code> actually holds the path of the folder containing the PDF, and not the PDF itself- How do I give it the name of the PDF file, when I don't actually know what the PDF file will be called?</p>