日期:2014-05-16  浏览次数:21256 次

C#记事本

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

namespace TXT
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        
        private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (richTextBox1.Modified)
            {
                /*提示保存对话框*/
                DialogResult dResult = MessageBox.Show("文件" + this.Text + "的内容已改变,需要保存吗?", "保存文件", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                switch (dResult)
                {
                    case DialogResult.Yes:
                        另存为ToolStripMenuItem_Click(null, null);
                        richTextBox1.Clear();
                        this.Text = "无标题-记事本";
                        break;
                    case DialogResult.No:
                        richTextBox1.Clear();
                        this.Text = "无标题-记事本";
                        break;
                    case DialogResult.Cancel:
                        break;
                }
            }
            else
            {
                richTextBox1.Clear();
                this.Text = "无标题-记事本";
                richTextBox1.Modified = false;
            }
        }

        private static string openfilepath = "";			//保存所打开文件的路径

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"; 	//设置文件类型   
            openFileDialog1.FilterIndex = 1;            	//设置默认文件类型的显示顺序
            openFileDialog1.RestoreDirectory = true;    	//打开对话框是否记忆上次打开的目录
            StreamReader sr = null;					//定义StreamReader对象
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    openfilepath = openFileDialog1.FileName;				//获取打开的文件路径
                    string name = openfilepath.Substring(openfilepath.LastIndexOf("\\") + 1);
                    this.Text = name;								//文件名作为标题
                    sr = new StreamReader(openfilepath, Encoding.Default);	//实例化sr
                    richTextBox1.Text = sr.ReadToEnd();					//读取所有文件内容
                }
                catch
                {
                    MessageBox.Show("打开文件时出错。", "错误",
                       System.Windows.Forms.MessageBoxButtons.OK,
                       System.Windows.Forms.MessageBoxIcon.Warning);
                    return;
                }
                finally
                {
                    if (sr != null)
                    {
                        sr.Close();				//关闭对象sr
                        sr.Dispose();			//释放对象sr资源
                    }
                }
            }
        }

        private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"; //设置文件类型   
            saveFileDialog1.FilterIndex = 2;		//设置默认文件类型的显示顺序
            saveFileDialog1.RestoreDirectory = true;	//保存对话框是否记忆上次打开的目录
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                openfilepath = saveFileDialog1.FileName.ToString(); //获取文件路径   
                FileStream fs;
                try
                { fs = File.Create(openfilepath); }
                catch
                {
                    MessageBox.Show("建立文件时出错。", "错误",
                        System.Windows.Forms.MessageBoxButtons.OK,
                        System.Windows.