Home > Mobile >  How do I overwrite a file if it already exists?
How do I overwrite a file if it already exists?

Time:04-23

I'm making a text editor using fastColoredTextbox. I have a button that allows you to save your text onto your pc. The problem is that it throws an exception when the user tries to save the file as a file that already exists, instead of overwriting the file.

This is my code.

SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt|*.txt|All files|*.*";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
    using (Stream s = File.Open(saveFileDialog1.FileName, FileMode.CreateNew))
    using (StreamWriter sw = new StreamWriter(s))
    {
        sw.Write(fastColoredTextBox1.Text);
    }
}

How would I go about making it overwrite the file if it already exists?

CodePudding user response:

Maybe this could help you:

sw = new StreamWriter(path, true);
sw.WriteLine(line);

https://docs.microsoft.com/es-es/dotnet/api/system.io.streamwriter.-ctor?view=net-6.0#system-io-streamwriter-ctor(system-string-system-boolean)

  • Related