简易文本编辑器

来源:互联网 发布:棉花期货天狼星软件 编辑:程序博客网 时间:2024/04/30 06:38
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.IO; namespace FileOptionApplication{    public partial class Form4 : Form    {        public Form4()        {            InitializeComponent();        }        private static string directory_path = "c:\\";        /// <summary>        /// 创建文本文件        /// </summary>        private void button1_Click(object sender, EventArgs e)        {            try            {                if (textBox1.Text.Length == 0)                {                    MessageBox.Show("文件名禁止为空!", "警报");                }                else                {                    directory_path = directory_path + textBox1.Text.Trim() + ".txt";                    //File.CreateText(..)返回的是一个StreamWriter                    StreamWriter sw = File.CreateText(directory_path);                    button2.Enabled = true;                    button3.Enabled = true;                    button1.Enabled = false;                    richTextBox1.Enabled = true;                    MessageBox.Show("文件文件成功建立。", "消息");                    sw.Close();                }            }            catch (Exception mm)            {                MessageBox.Show("磁盘操作错误,原因:" + Convert.ToString(mm), "警报");            }        }        /// <summary>        /// 打开文本文件        /// </summary>        private void button2_Click(object sender, EventArgs e)        {            try            {                OpenFileDialog open = new OpenFileDialog();//创建一个打开的对话框                open.Title = "打开文本文件";                open.FileName = "";                open.AddExtension = true;//设置是否自动在文件中添加扩展名                open.CheckFileExists = true;//检查文件是否存在                open.CheckPathExists = true;//验证路径有效性                open.Filter = "文本文件(*.txt)|*.txt";//设置将打开文件的类型                open.ValidateNames = true;                //文件有效性验证ValidateNames,验证用户输入是否是一个有效的Windows文件名                if (open.ShowDialog() == DialogResult.OK)                {                 StreamReader sr = new StreamReader(open.FileName, System.Text.Encoding.Default);                    this.richTextBox1.Text = sr.ReadToEnd();                }                MessageBox.Show("文件打开成功。", "消息");            }            catch (Exception mm)            {                MessageBox.Show("磁盘操作错误,原因:" + Convert.ToString(mm), "警报");            }        }         /// <summary>        /// 保存编辑文件        /// </summary>        private void button3_Click(object sender, EventArgs e)        {            try            {        FileStream textfile = File.Open(directory_path, FileMode.OpenOrCreate, FileAccess.Write);                StreamWriter sw = new StreamWriter(textfile, Encoding.GetEncoding("GB2312"));                sw.Write(richTextBox1.Text.ToString());                MessageBox.Show("文件写成功。", "警报");            }            catch (Exception mm)            {                MessageBox.Show("磁盘操作错误,原因:" + Convert.ToString(mm), "警报");            }        }    }}

0 0