using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Ex06_3
{
    public partial class Form1 : Form
    {
        
        string myfile = "";

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            StreamReader myread = new StreamReader(openFileDialog1.FileName, Encoding.Default);
            try
            {
                textBox1.Text = "";
                string mys = myread.ReadLine();
                while (mys != null)
                {
                    textBox1.Text = textBox1.Text + mys + "\r\n";
                    mys = myread.ReadLine();
                }
                myfile = openFileDialog1.FileName;
            }
            catch (Exception mye)
            {
                MessageBox.Show("读取文件失败！" + mye.Message);
            }
            finally
            {
                myread.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK && saveFileDialog1.FileName != "")
            {
                myfile = saveFileDialog1.FileName;
            }

            //第二个参数为false代表改写内容，不是追加
            StreamWriter Writer = new StreamWriter(myfile, false, Encoding.Default);
            try
            {
                foreach (string line in textBox1.Lines)
                {
                    Writer.Write(line + "\n", Encoding.Default);
                }

                Writer.Flush();//将缓冲区的数据写入流
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存文件失败！" + ex.Message);
            }
            finally
            {
                Writer.Close();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
            Application.Exit();
        }
    }
}
