Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can I save the contents of a rich text box without needing to open the save file dialog.
i think its something like:

richTextBox1.SaveFile(@"\Documents\save_file_here.rtf");

but it cant find the file to overwrite in the first place.
Posted
Updated 20-Feb-22 8:05am

The shortest way is probably:

File.WriteAllText(@"\Documents\save_file_here.rtf", richTextBox1.Text)


Not sure if that will save formatting though.
 
Share this answer
 
Comments
vlad781 30-Jun-11 18:02pm    
file dos not exist in the current context.
Valery Possoz 30-Jun-11 18:14pm    
did you add at the beginning of the file : using System.IO ;
vlad781 30-Jun-11 18:18pm    
oh ya. huh. all good now, many thanks.
//create a file first
FileStream outStream = File.Create("Path \\filename");

//Create a variable
string data = richTextBox.Text;

//Save 
StreamWriter sw = new StreamWriter(outStream);
        sw.WriteLine(data);
        sw.Flush();
        sw.Close();
 
Share this answer
 
Comments
vlad781 30-Jun-11 18:03pm    
filestream could not be found.
Member 15541893 20-Feb-22 14:06pm    
rich textboxes HASN'T .Text method! GEESH!!
You have a problem with separation of concerns. Dialog boxes have nothing to do with file read/write operations. They only work with file system and allows to determine a file name to work with, nothing else. For read/write operations, it's absolutely irrelevant where the file name came from.

Also, there absolutely no situations where a file named hard-coded in the program could be useful, unless you run programs from the Visual Studio and modify them all the time. If you try to deploy executable, it will be defunct due to hard-coding problems. File names (even constant) are always calculated during run-time.

—SA
 
Share this answer
 
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.RichText);
}

chibre de dingue
 
Share this answer
 
Comments
Richard Deeming 18-Sep-18 12:56pm    
Read the question again: "without needing to open the save file dialog"

Also, the question was about saving the file; your answer is about loading the file.

The question has already been solved. An unformatted code block which ignores the requirements from the question adds nothing to the discussion.
Stream mys;

OpenFileDialog opf1 = new OpenFileDialog();
opf1.Title = "Open Text File";
opf1.Filter = "text files|*.txt|All Files(*.*)|*.*|Html(*html*)|*html*";

if (opf1.ShowDialog()==System.Windows.Forms.DialogResult.OK)
{
if((mys=opf1.OpenFile())!=null)
{
string strf = opf1.FileName;
string fltxt = File.ReadAllText(strf);
richTextBox1.Text = fltxt;
}
 
Share this answer
 
Comments
Maciej Los 29-Sep-20 7:28am    
Why to post an answer to such of old question and where the question has been already answered? Doubt that OP is not interesting in finding new answer.
BTW: your code is used to read text file, instead of write it. That's the reason of vote of 1.
Richard Deeming 29-Sep-20 9:02am    
Read the question. The OP wants to SAVE a file WITHOUT opening the file dialog box.

Your "solution" does nothing of the sort.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900