Click here to Skip to main content
15,914,820 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have to create multiple text files.

If the already exists I need to add count after the filename.

Example

If I Enter test.txt

If file exists then it should rename as test(1).txt.

If I again give test.txt it should rename as test(2).txt.

How to do this using C#
Posted

1 solution

Create the filename with
C#
string filename = System.IO.Path.Combine(folder, string.Format("Test({0}).txt", i));

and check for its existence with
C#
System.IO.File.Exists(filename);

till that returns false.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900