If File Exists Append Version To End Of FileName

  • Thread starter Thread starter IndigoMontoya
  • Start date Start date
I

IndigoMontoya

Guest
I have a winform that will run a few queries and write data to Excel, always saving the workbook with the same name. This can become problematic if a user runs the procedure twice w/o moving the 1st workbook, so I thought I would slightly alter the naming convention. This is what I need to do:

//Set the filename

string filename = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+"\\Test";

//Check if file exists

if (File.Exists(filename)

//if file does exist - get a count of the files

string target = "Test*";

var matchingFiles = Directory.EnumerateFiles(path, target, SearchOption.AllDirectories);
int count = matchingFiles.Count();

//Now append the count + 1 to the filename so you know it is the next iteration

filename = filename + count + 1 + ".xlsx"


Now of course this is just pseudocode, how would this be achieved using C# & VS 2013?

Continue reading...
 
Back
Top