Practical Programmer
Practical Programmer

Reputation: 315

How to build file-name for File.WriteAllText dynamically with random number in it?

I have numerous files and I want to write at a file randomly, what I want to do is this:

StringBuilder sb = new Stringbuilder(); 
int x;
Random rnd = new Random();

 x = rnd.Next(1,10);

File.WriteAllText("C:\\Top Folder\\File Folder\\file{0}.dat",x.ToString(),sb.ToString());

I know the argument on that WriteAllText is wrong, but that is the idea of what I want to do; Put whatever random number that is generated into {0} then write the contents of the Stringbuilder inside the file.

Help is much appreciated. Thanks in advance :D

Upvotes: 1

Views: 1079

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460138

You can use String.Format:

string file = string.Format("C:\\Top Folder\\File Folder\\file{0}.dat", x);
File.WriteAllText(file, sb.ToString());

Upvotes: 5

Related Questions