I have a string with a C# program that I want to write to a file and always overwrite the existing content. If the file isn\'t there, the program should create a new file i
Use the File.WriteAllText method. It creates the file if it doesn't exist and overwrites it if it exists.
Generally, FileMode.Create is what you're looking for.
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.
System.IO.File.WriteAllText (@"D:\path.txt", contents);