StreamReader和StreamWriter类的基本操作

来源:互联网 发布:淘宝手机号绑定怎么改 编辑:程序博客网 时间:2024/06/05 03:23


using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.IO;using System.Windows.Forms;namespace streamRWFormDemo{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            openFileDialog1.Filter = "文本文件(*.txt)|*.txt";            if (openFileDialog1.ShowDialog() == DialogResult.OK)            {                richTextBox1.Text = "";                string path = openFileDialog1.FileName;                StreamReader sr = new StreamReader(path, Encoding.Default);                richTextBox1.Text = sr.ReadToEnd();                sr.Close();            }        }        private void button2_Click(object sender, EventArgs e)        {            if (richTextBox1.Text == "")                MessageBox.Show("文件内容不能为空!");            else            {                saveFileDialog1.Filter = "文本文件(*.txt)|*.txt";                if (saveFileDialog1.ShowDialog() == DialogResult.OK)                {                    string path = saveFileDialog1.FileName;                    StreamWriter sw = new StreamWriter(path);                    sw.Write(richTextBox1.Text);                    sw.Close();                }            }        }    }}



0 0
原创粉丝点击