C # Open and save files with OpenFileDialog files and SaveFileDialog

Source: Internet
Author: User

First, open the file

Stream mystream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog ();
Openfiledialog1.initialdirectory = "d:\\";
Openfiledialog1.filter = "ext files (*.txt) |*.txt| All Files (*. *) |*>** ";
Openfiledialog1.filterindex = 2;
Openfiledialog1.restoredirectory = true;
if (openfiledialog1.showdialog () = = DialogResult.OK)
{
Try
{
if ((MyStream = Openfiledialog1.openfile ()) = null)
{
StreamReader st = new StreamReader (Openfiledialog1.filename, encoding.getencoding ("gb2312"));
String str = St. ReadLine ();
while (str! = NULL)
{
Richtextbox1.appendtext (str);
Richtextbox1.appendtext ("\ n");
str = St. ReadLine ();
}
}

}catch (Exception ex) {
MessageBox.Show ("Error opening file:" +ex. Message);
}

Second, save the file

SaveFileDialog SaveFileDialog1 = new SaveFileDialog ();
Savefiledialog1.initialdirectory = "d:\\";
Savefiledialog1.filter = "ext files (*.txt) |*.txt| All Files (*. *) |*>** ";
Savefiledialog1.filterindex = 2;
Savefiledialog1.restoredirectory = true;
DialogResult dr = Savefiledialog1.showdialog ();
if (dr = = DialogResult.OK && saveFileDialog1.FileName.Length > 0)
{
Richtextbox1.savefile (Savefiledialog1.filename,richtextboxstreamtype.plaintext);
MessageBox.Show ("Save file Successfully!") "," Save the File ");
}

Three, Color dialog box

ColorDialog colorDialog1 = new ColorDialog ();
Colordialog1.allowfullopen = false;
colorDialog1.Color = Richtextbox1.forecolor;
Colordialog1.showhelp = true;
if (Colordialog1.showdialog () ==dialogresult.ok)
{
Richtextbox1.forecolor = colorDialog1.Color;

}

Four, Font dialog box

FontDialog FontDialog = new FontDialog ();
Fontdialog.font = Richtextbox1.font;
Fontdialog.color = Richtextbox1.forecolor;
if (Fontdialog.showdialog ()! = DialogResult.Cancel)
{
Richtextbox1.font = Fontdialog.font;
Richtextbox1.forecolor = Fontdialog.color;
}

C # Open and save files with OpenFileDialog files and SaveFileDialog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.