daf32 daf32 - 1 year ago 91
C# Question

I want to create a new text file with C#

I want to create new text files everytime the user clicks the button, so if the user clicks 5 times the button, I want make 5 new text files automatically. How can I do that?

Right now I have this but this doesn't work:

StreamWriter myOutputStream = new StreamWriter(@"C: \Users\LH\Desktop\test.txt");

foreach (var item in listBox1.Items)


Answer Source

You need have a naming convention / rule for your files.

For example: when using date and time you can use:

string filePath = DateTime.Now.ToString();
string content = "somecontent";

if (!File.Exists(filepath))
  System.IO.File.WriteAllText(filepath, content);

Put that code in your loop according to your specific scenario.

Remember, this is only if you want to use date time for filepath. You can use a counter, name list with counter, prefixes or suffixes, etc.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download