title: How to rename a file if it already exists in directory (ASP.net)
body: <p>I have seen quite a few file uploads questions online, but it hasnt seemed to work for me.
  I need to rename my file if it already exists in the directory. </p>
  
  <p>I have even tried MSDN save as/rename method but it doesn't seem to work, maybe I'm missing something. I dont want to delete or override anything guys.</p>
  
  <p>This is what I'm using so far, but it just overwrites the files</p>
  
  <pre><code>string folderName = Server.MapPath("\\Track\\Upload");    
  string pathString = System.IO.Path.Combine(folderName, SupDDL.SelectedItem.Text.Trim());
  System.IO.Directory.CreateDirectory(pathString);
  string fileName = QuoteUpload.FileName;
  if (!System.IO.File.Exists(pathString))
  {
  using (System.IO.FileStream fs = System.IO.File.Create(pathString))
   {
     for (byte i = 0; i &lt; 100; i++)
    {
     fs.WriteByte(i);
     }
  QuoteUpload.SaveAs(pathString)}
  }
  </code></pre>
  
  <p>Excuse me if I haven't given enough details, just let me know if you would like more information.</p>
  
  <p><strong>UPDATED</strong></p>
  
  <p>So I have updated it to this, it does the job, but I would like it to automatically increment numbers, instead of just adding the 2 once.</p>
  
  <pre><code>int count = 2;
  string fileNameOnly = Path.GetFileNameWithoutExtension(pathString);
  string extension = Path.GetExtension(pathString);
  string path = Path.GetDirectoryName(pathString);
  string newPath = pathString;
  
  if (!System.IO.File.Exists(pathString))
  {
  QuoteUpload.SaveAs(pathString);
  }
  else {
  string tempName = string.Format("{0}({1})", fileNameOnly, count++);
  newFullPath = Path.Combine(path, tempFileName + extension);
  QuoteUpload.SaveAs(newPath); }
  }
  </code></pre>