private static string GenerateFileName()
{
string dateFormat = DateTime.Now.ToString("yyyyMMdd_HHmmss");
string filePath = Path.Combine(Paths.DocumentsFolder, "ExceptionLog_" + dateFormat + ".txt");
// If the file already exists, then we try to create a duplicate with a numerical extension "_(1)"
if (File.Exists(filePath))
{
filePath = Path.Combine(Paths.DocumentsFolder, "ExceptionLog_" + dateFormat + "_({0}).txt");
// We will only try and create up to 12 exceptions during this timestamp
int maxIndex = Enumerable.Range(1, 12)
.SkipWhile(x => File.Exists(String.Format(filePath, x)))
.FirstOrDefault();
filePath = String.Format(filePath, maxIndex);
}
return filePath;
}