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.
解决方案
The shortest way is probably:
File.WriteAllText(@"\Documents\save_file_here.rtf", richTextBox1.Text)
Not sure if that will save formatting though.
//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();
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