title: How to shorten a Windows filename programmatically - "Bitly for filenames"?
body: <p>Is there an easy way to take a very long filename (which contains parameters and settings that were used to generate the file) and shorten it programmatically so it will save on Windows?</p>
  
  <p><strong><em>The filename cannot be sent to a URL shortening service (Bitly, Google, etc.) because the information is confidential and the network is segregated.</em></strong></p>
  
  <p><strong><em>The filename has to be shortened internally within the application and without using  third-party library and without sending the result to a database.</em></strong></p>
  
  <p>e.g. Start with a file name so long that Windows will fail to save it:</p>
  
  <pre><code>E:\Results\Job\&lt;SomeJobComponentName&gt;\[A very long file name with the parameters and settings that were used to generate it].csv
  </code></pre>
  
  <p>And save it as</p>
  
  <pre><code>E:\Results\Job\&lt;SomeJobComponentName&gt;\0eVfd878swg9.csv
  </code></pre>
  
  <p>And then when the file is read, the code can convert</p>
  
  <pre><code>0eVfd878swg9.csv
  </code></pre>
  
  <p>back to</p>
  
  <pre><code>[A very long file name with the parameters and settings that were used to generate it].csv
  </code></pre>
  
  <p>Sort of like a Bitly for filenames.</p>
  
  <p>This is not about encrypting the CONTENT of the file.  It's simply about making sure that filenames of any length can be saved without hindrance.  The filename needs to be convertible back to the original without opening the file, so that the un-shortened filename can be parsed and viewed in a viewer (which the user uses to browse the result files in the job directory).</p>
  
  <p>I've previously tried the "\?\" prefix to the full path but for whatever reason this fails when using StreamWriter.  It can't handle this workaround.</p>
  
  <p>Let me know if I'm barking up the wrong tree here!</p>
  
  <p>Using C# 7.0, Windows Server 2012 R2 and Windows 10 desktop.</p>