I’m trying to create .txt files with my program and saving the contents of a richtextbox in them (or simply editing existing files, but this already works). The problem is that when I save try saving the file, it creates the file but without the .txt extension. I’ve tried a couple of things, including the following two, but both create a file without extension. How can I fix this?
SaveFileDialog op = new SaveFileDialog();
op.Title = "Save";
op.Filter = "Text Document(*.txt)|*txt";
//op.DefaultExt = "txt";
//op.AddExtension = true;
if (op.ShowDialog() == DialogResult.OK)
{
using (Stream s = File.Open(op.FileName, FileMode.CreateNew))
using (StreamWriter sw = new StreamWriter(s))
{
sw.Write(richTextBox1.Text);
}
}
And:
SaveFileDialog op = new SaveFileDialog();
op.Title = "Save";
op.Filter = "Text Document(*.txt)|*txt";
if (op.ShowDialog() == DialogResult.OK)
{
richTextBox1.SaveFile(op.FileName, RichTextBoxStreamType.PlainText);
filename = op.FileName;
this.Text = op.FileName;
}
You need to add a dot before the extension here:
So it becomes this: