title: How do I cast from System.Web.HttpPostedFileBase to System.Web.HttpPostedFile?
body: <p>While trying to implement an MVC file upload <a href="http://www.hanselman.com/blog/ABackToBasicsCaseStudyImplementingHTTPFileUploadWithASPNETMVCIncludingTestsAndMocks.aspx" rel="noreferrer">example</a> on Scott Hanselman's blog. I ran into trouble with this example code:</p>
  
  <pre><code>foreach (string file in Request.Files)
  {
     HttpPostedFile hpf = Request.Files[file] as HttpPostedFile;
     if (hpf.ContentLength == 0)
        continue;
     string savedFileName = Path.Combine(
        AppDomain.CurrentDomain.BaseDirectory, 
        Path.GetFileName(hpf.FileName));
     hpf.SaveAs(savedFileName);
  }
  </code></pre>
  
  <p>I converted it to VB.NET:</p>
  
  <pre><code>For Each file As String In Request.Files
      Dim hpf As HttpPostedFile = TryCast(Request.Files(file), HttpPostedFile)
      If hpf.ContentLength = 0 Then
          Continue For
      End If
      Dim savedFileName As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.GetFileName(hpf.FileName))
      hpf.SaveAs(savedFileName)
  Next
  </code></pre>
  
  <p>But I am getting an invalid cast exception from the compiler:</p>
  
  <pre><code>Value of type 'System.Web.HttpPostedFileBase' cannot be converted to 'System.Web.HttpPostedFile'.
  </code></pre>
  
  <p>Hanselman posted his example on 2008-06-27, and I assume it worked at the time.  MSDN doesn't have any similar examples, so what gives?</p>