System.IO.File.WriteAllText (@"D:\path.txt", contents);
If your code doesn't require the file to be truncated first, you can use the FileMode.OpenOrCreate to open the filestream, which will create the file if it doesn't exist or open it if it does. You can use the stream to point at the front and start overwriting the existing file?
I'm assuming your using a streams here, there are other ways to write a file.
Generally, FileMode.Create
is what you're looking for.
Use the File.WriteAllText
method. It creates the file if it doesn't exist and overwrites it if it exists.
Source: Stackoverflow.com