title: How do you correctly escape a document name in .NET?
body: <p>We store a bunch of weird document names on our web server (people upload them) that have various characters like spaces, ampersands, etc.  When we generate links to these documents, we need to escape them so the server can look up the file by its raw name in the database.  However, none of the built in .NET escape functions will work correctly in all cases.</p>
  
  <p>Take the document <code>Hello#There.docx</code>:</p>
  
  <p><code>UrlEncode</code> will handle this correctly:</p>
  
  <pre><code>HttpUtility.UrlEncode("Hello#There");
  "Hello%23There"
  </code></pre>
  
  <p>However, <code>UrlEncode</code> will <em>not</em> handle <code>Hello There.docx</code> correctly:</p>
  
  <pre><code>HttpUtility.UrlEncode("Hello There.docx");
  "Hello+There.docx"
  </code></pre>
  
  <p>The <code>+</code> symbol is only valid for URL parameters, not document names.  Interestingly enough, this actually works on the Visual Studio test web server but not on IIS.</p>
  
  <p>The <code>UrlPathEncode</code> function works fine for spaces:</p>
  
  <pre><code>HttpUtility.UrlPathEncode("Hello There.docx");
  "Hello%20There.docx"
  </code></pre>
  
  <p>However, it will not escape other characters such as the <code>#</code> character:</p>
  
  <pre><code>HttpUtility.UrlPathEncode("Hello#There.docx");
  "Hello#There.docx"
  </code></pre>
  
  <p>This link is invalid as the <code>#</code> is interpreted as a URL hash and never even gets to the server.</p>
  
  <p>Is there a .NET utility method to escape all non-alphanumeric characters in a document name, or would I have to write my own?</p>