in csharp i have a string and i want to write it to a file and always overwrite the existing content. if the file isn't there it should create it as well (instead of throwing an exception)
+6
A:
System.IO.File.WriteAllText (@"D:\path.txt", contents);
Please make sure you have appropriate privileges to write at the location, otherwise you will get an exception.
Hemant
2009-08-04 05:56:44
+1
A:
Use the File.WriteAllText
method. It creates the file if it doesn't exist and overwrites it if it exists.
Guffa
2009-08-04 05:59:17
A:
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.
Spence
2009-08-04 06:05:44