C#修改txt文本内容的方法

 

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;
using BarCode.Logs;

namespace BarCode.Else
{
    public partial class EventRecord : Form
    {
        string path = Application.StartupPath + "\\Else\\Memorandum.ini";

        public EventRecord()
        {
            InitializeComponent();
        }

        private void EventRecord_Load(object sender, EventArgs e)
        {
            file_open();
        }

        private void file_open()
        {
            //打开系统日志文件
            if (!File.Exists(path))
            {
                // 目录/文件不存在,建立目录/文件
                File.CreateText(path);
            }
            //打开文件
            //FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            //通过指定字符编码方式可以实现对汉字的支持,否则在用记事本打开查看会出现乱码
            StreamReader m_streamReader = new StreamReader(path, System.Text.Encoding.GetEncoding("GB2312"));
            //使用StreamReader类来读取文件
            m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);

            // 从数据流中读取每一行,直到文件的最后一行,并在richTextBox1中显示出内容
            this.richTextRecord.Text = "";
            string strLine = m_streamReader.ReadLine();

            while (strLine != null)
            {

                this.richTextRecord.Text += strLine + "\n";

                strLine = m_streamReader.ReadLine();
            }
            //关闭此StreamReader对象

            m_streamReader.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SaveToTxtFile();
        }

        /// <summary>
        /// 保存文件
        /// </summary>
        private void SaveToTxtFile()
        {
            //int buffer = 1024;
            //创建一个文件流,用以写入或者创建一个StreamWriter

            //FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);

            //通过指定字符编码方式可以实现对汉字的支持,否则在用记事本打开查看会出现乱码
            /*改函数很重要由于要将修改的内容覆盖原来的文件内容故设第二个参数为false,
            同时还不能用其他类如FileString来操作文件(多线程占用,不能覆盖修改文件)
             * 例如:new StreamWriter(fs, true, Encoding.GetEncoding("GB2312"));是错误的*/
            StreamWriter m_streamWriter = new StreamWriter(path, false, Encoding.GetEncoding("GB2312"));

            m_streamWriter.Flush();

            // 使用StreamWriter来往文件中写入内容

            m_streamWriter.BaseStream.Seek(0, SeekOrigin.Begin);

            // 把richTextBox1中的内容写入文件

            m_streamWriter.Write(richTextRecord.Text);

            //关闭此文件

            m_streamWriter.Flush();

            m_streamWriter.Close();

            this.labwarm.Text = "保存成功!";
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            this.labwarm.Text = "正在编辑文本";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SavefileAs();
        }
        /// <summary>
        /// 另存为
        /// </summary>
        private void SavefileAs()
        {
            SaveFileDialog savefile = new SaveFileDialog();
            //提示用户选择保存文件(默认为桌面)
            savefile.InitialDirectory = @"C:\Documents and Settings\Administrator\桌面";
            //打开文件的初始目录
            savefile.Filter = "备忘录 (*.txt)|";
            savefile.FileName = "Memorandum";
            //格式转换如此简单
            savefile.DefaultExt = ".txt";
            //设置或获取文件后缀
            DialogResult dr = savefile.ShowDialog();
            if (dr == DialogResult.OK)
            {
                String filePath = savefile.FileName.ToString();
                try
                {
                    File.Copy(path, filePath, true);
                }
                catch (Exception e1)
                { (new Clogs()).LogError("错误信息", e1); return; }
            }
        }
    }
}

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页