UPDATE: I just got another solution here: https://stackoverflow.com/a/1078898/14311474
I'm having a little problem with incrementing the current existing file.
instead of incrementing the current count, the filename becomes like this.
outputFileName(1).mp4
outputFileName(1)(1).mp4
outputFileName(1)(1)(1).mp4
I want the filename become like this:
outputFileName(1).mp4
if exists:
outputFileName(2).mp4
if exists:
outputFileName(3).mp4
Here's my code:
private void OverWriteFile(string OutputFileName)
{
bool r = false;
if (!string.IsNullOrEmpty(OutputFileName)) {
if (File.Exists(OutputFileName)) {
var dr = MessageBox.Show($"The file '{finalOutputVideoFilePath}' already exists. Overwrite? else \nfile name will be incremented", "Overwriting",
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("{0}({1}){2}", fileNameOnly, i, extension);
}
return fileName;
}