Question: I have an ini file which I need to clear before I add info to it.
Unfortunately, if I just delete the file, the permissions are gone as well.
Is there a way to delete a file's content without deleting the file ?
String path = "c:/file.ini";
using (var stream = new FileStream(path, FileMode.Truncate))
{
using (var writer = new StreamWriter(stream))
{
writer.Write("data");
}
}
Just open the file in truncate (i.e. non-append) mode and close it. Then its contents are gone. Or use the shortcut in the My
namespace:
My.Computer.FileSystem.WriteAllText("filename", "", False)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With