C#实现一个简单的记事本程序

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

namespace 测试0
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog FiL0 = new SaveFileDialog();  // 创建对象  保存方式
            FiL0.Filter = "文本文件(*.txt)|*.txt";     //设置保存格式
            FiL0.RestoreDirectory = true;                //保存对话框是否记忆上次打开的目录
            if (FiL0.ShowDialog() == DialogResult.OK)    //如果显示窗口打开
            {
                StreamWriter sw = new StreamWriter(FiL0.FileName,true);  //实例化wtreamWriter 对象
                sw.WriteLine(textBox1.Text);             //写入内容
                sw.Close();                              //关闭当前写入
            }
             
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Environment.Exit(0);   //退出
        }

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog OpFi = new OpenFileDialog();  // 创建对象   打开方式
            OpFi.Filter = "文本文件(*.txt)|*.txt";     //设置保存格式
            OpFi.RestoreDirectory = true;                //保存对话框是否记忆上次打开的目录
            if (OpFi.ShowDialog() == DialogResult.OK)    //如果显示窗口打开
            {
                textBox1.Text = string.Empty;      //清空 textBox1.Text
                StreamReader   sw = new StreamReader(OpFi.FileName, true);    //实例化wtreamWriter 对象
                textBox1.Text = sw.ReadToEnd();    //显示内容
                sw.Close();                        //关闭当前操作
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/heyics/article/details/80003568