title: increment existing filename if exists
body: <p>UPDATE: I just got another solution here:
  <a href="https://stackoverflow.com/a/1078898/14311474">https://stackoverflow.com/a/1078898/14311474</a></p>
  <p>I'm having a little problem with incrementing the current existing file.</p>
  <p>instead of incrementing the current count, the filename becomes like this.</p>
  <pre><code>outputFileName(1).mp4
  outputFileName(1)(1).mp4
  outputFileName(1)(1)(1).mp4
  </code></pre>
  <p>I want the filename become like this:</p>
  <pre><code>outputFileName(1).mp4
  if exists:
  outputFileName(2).mp4
  if exists:
  outputFileName(3).mp4
  </code></pre>
  <p>Here's my code:</p>
  <pre><code>private void OverWriteFile(string OutputFileName)
  {
      bool r = false;
      if (!string.IsNullOrEmpty(OutputFileName)) {
          if (File.Exists(OutputFileName)) {
              var dr = MessageBox.Show($&quot;The file '{finalOutputVideoFilePath}' already exists. Overwrite? else \nfile name will be incremented&quot;, &quot;Overwriting&quot;,
                                      MessageBoxButtons.YesNo, MessageBoxIcon.Question);
              if (dr == DialogResult.No) { 
              
              OutputFileName.GetNextFileName();
              
              }
              else {
                  File.Delete(OutputFileName);
              }
          }
      }
      return r;
  }
  
  public static string GetNextFileName(this string fileName)
  {
      string extension = Path.GetExtension(fileName);
      string pathName = Path.GetDirectoryName(fileName);
      string fileNameOnly = Path.Combine(pathName, Path.GetFileNameWithoutExtension(fileName));
      int i = 1;
      while (File.Exists(fileName))
      {
          Interlocked.Increment(ref i);
          fileName = string.Format(&quot;{0}({1}){2}&quot;, fileNameOnly, i, extension);
      }
      return fileName;
  }
  </code></pre>