I looked all over Google, but could not find anything other than:

File.WriteAllLines();

It does not work, it just appends it.
Cheers.

Recommended Answers

All 2 Replies

FileStream f = File.Open(@"D:\TestData.txt", FileMode.Create);
f.Close();
commented: n/a +1
FileStream f = File.Open(@"D:\TestData.txt", FileMode.Create);
f.Close();

Momerath's method will do what you require if all you need is the file to be emptied.

Essentially what it accomplishes is that it will open the file in create mode which makes any existing file with the same name be deleted and replaced with the new file. Further, the code closes the stream without adding to the new file leaving you with an empty new file.

If, on the other hand, you require some of the details of the file to remain then you will need to first read the details into a variable, prune off the unnecessary bits, then create the new same-name file and insert the trimmed contents.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.