The Insert key on your keyboard allows you to replace text as you type. You can set up the function in Word Options.
If you want to overwrite the text file, then you can use the File. WriteAllText method. You just need to supply the full path and filename of the text file in the first parameter. Supply the Text that you want to write to the file in the second parameter.
StreamWriter(String, Boolean) Initializes a new instance of the StreamWriter class for the specified file by using the default encoding and buffer size. If the file exists, it can be either overwritten or appended to.
System.IO.File.WriteAllText (@"D:\path.txt", contents);
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.
Use the file mode enum to change the File.Open
behavior. This works for binary content as well as text.
Since FileMode.Open
and FileMode.OpenOrCreate
load the existing content to the file stream, if you want to replace the file completely you need to first clear the existing content, if any, before writing to the stream. FileMode.Truncate
performs this step automatically
// OriginalFile:
oooooooooooooooooooooooooooooo
// NewFile:
----------------
// Write to file stream with FileMode.Open:
----------------oooooooooooooo
var exists = File.Exists(path);
var fileMode = exists
? FileMode.Truncate // overwrites all of the content of an existing file
: FileMode.CreateNew // creates a new file
using (var destinationStream = File.Open(path, fileMode)
{
await newContentStream.CopyToAsync(destinationStream);
}
FileMode Enum
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