title: c# code to find folder which contains a group of files and sub folders
body: <p>I would like to find a function to find the folder which contains a group of files and folders. For example, for a collection of files as follows:</p>
  
  <pre><code>C:\Users\Simon\Documents\TestFolder\File.doc
  C:\Users\Simon\Documents\TestFolder\File2.doc
  C:\Users\Simon\Documents\TestFolder\Subfolder1\File.doc
  C:\Users\Simon\Documents\TestFolder\Subfolder1\Subfolder2\File.doc
  C:\Users\Simon\Documents\TestFolder\Subfolder1\Subfolder2\Subfolder3
  </code></pre>
  
  <p>The function would return the folder </p>
  
  <pre><code>C:\Users\Simon\Documents\TestFolder
  </code></pre>
  
  <p>And for the collection</p>
  
  <pre><code>C:\Users\Simon\Documents\File.doc
  C:\Users\Simon\Documents\TestFolder\File.doc
  C:\Users\Simon\Documents\TestFolder\File2.doc
  C:\Users\Simon\Documents\TestFolder\Subfolder1
  </code></pre>
  
  <p>The function would return:</p>
  
  <pre><code>C:\Users\Simon\Documents
  </code></pre>
  
  <p>In the case where there is no common folder such as:</p>
  
  <pre><code>Z:\Backup\File.doc
  C:\Users\Simon\Documents\TestFolder\File.doc
  C:\Users\Simon\Documents\TestFolder\File2.doc
  C:\Users\Simon\Documents\TestFolder\Subfolder1\File.doc
  C:\Users\Simon\Documents\TestFolder\Subfolder1\Subfolder2\File.doc
  </code></pre>
  
  <p>The function would return null. Does anyone have such a function?</p>