Мой код С# генерирует несколько текстовых файлов на основе ввода и сохраняет их в папке. Кроме того, я предполагаю, что имя текстового файла будет таким же, как и вход (вход содержит только буквы) Если два файла имеют одинаковое имя, он просто перезаписывает предыдущий файл. Но я хочу сохранить оба файла.
Я не хочу добавлять текущее время или случайное число ко второму имени файла. Вместо этого я хочу сделать это так же, как Windows. Если имя файла fisrt равно AAA.txt, второе имя файла - AAA (2).txt, третьим именем будет AAA (3).txt..... N-м именем файла будет AAA (N).txt.
string[] allFiles = Directory.GetFiles(folderPath).Select(filename => Path.GetFileNameWithoutExtension(filename)).ToArray();
foreach (var item in allFiles)
{
//newFileName is the txt file which is going to be saved in the provided folder
if (newFileName.Equals(item, StringComparison.InvariantCultureIgnoreCase))
{
// What to do here ?
}
}