c#读写txt文档代码

来源:互联网 发布:沈阳搜索引擎排名数据 编辑:程序博客网 时间:2024/05/29 02:14

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 System.Text.RegularExpressions;  

 

namespace ReadWriteTxt

{

    public partial class Form1 : Form

    {

        string fileName;

        System.IO.StreamReader st;//文件读取器

        int num_of_line =-1;//读取的行数

        string wholeTxt;//存放整体文件

        string TxtToWrite;//存放要写入的数据

        public Form1()

        {

            InitializeComponent();

        }

 

        private void label1_Click(object sender, EventArgs e)

        {

 

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

 

                ////////////打开文本

                DataSet myDs = new DataSet();

                OpenFileDialog openFileDialog = new OpenFileDialog();

                openFileDialog.InitialDirectory = "c://";//注意这里写路径时要用c://而不是c:/   

                openFileDialog.Filter = "txt文件|*.txt|所有文件|*.*";

                openFileDialog.RestoreDirectory = true;

                openFileDialog.FilterIndex = 1;

                openFileDialog.ShowDialog();

                ////////////获取文件名

                fileName = openFileDialog.FileName;

                ///////////读取文件

                if (openFileDialog.ShowDialog() == DialogResult.OK)

                {

                    st = new System.IO.StreamReader(fileName, System.Text.Encoding.UTF8);//UTF8为编码

                    wholeTxt = st.ReadToEnd();

                    st.Close();//关闭读入器

                    this.textBox1.Text = wholeTxt;

                }

 

        }

 

        private void textBox1_TextChanged(object sender, EventArgs e)

        {

 

        }

 

        private void button3_Click(object sender, EventArgs e)

        {

            num_of_line=num_of_line+1;

            if (num_of_line == 0)

            {

                ////////////打开文本

                DataSet myDs = new DataSet();

                OpenFileDialog openFileDialog = new OpenFileDialog();

                openFileDialog.InitialDirectory = "c://";//注意这里写路径时要用c://而不是c:/   

                openFileDialog.Filter = "txt文件|*.txt|所有文件|*.*";

                openFileDialog.RestoreDirectory = true;

                openFileDialog.FilterIndex = 1;

                openFileDialog.ShowDialog();

                ////////////获取文件名

                fileName = openFileDialog.FileName;

                ///////////读取文件

                if (openFileDialog.ShowDialog() == DialogResult.OK)

                {

                    //st = new System.IO.StreamReader(fileName, System.Text.Encoding.UTF8);//UTF8为编码

                    st = File.OpenText(fileName);

                    //wholeTxt = st.ReadToEnd();

                    //st.Close();//关闭读入器

                    //this.lineTxt = wholeTxt.Split('/r');//按行分割

                    //textBox1.Text = lineTxt[num_of_line]+"/r/n";/////注释掉的这部分内容是另一种读取方法:先整体读取,然后按行分割读取房贷lineTxt[]

                    textBox1.Text = st.ReadLine();

                }

            }

            else

            {

 

                if (st.ReadLine() != null)

                {

                    textBox1.Text = textBox1.Text + st.ReadLine();

                }

                else

                {

                    MessageBox.Show("已经读取到最后一行!");

                }

            }

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

 

            TxtToWrite = textBox1.Text;

            if (TxtToWrite == string.Empty)

            {

                MessageBox.Show("TextBox 内容不能为空!");

            }

            else

            {

                SaveFileDialog savefiledialog = new SaveFileDialog();

                savefiledialog.Filter = "文本文件(*.txt)|*.txt";

                if (savefiledialog.ShowDialog() == DialogResult.OK)

                {

                    //使用“另存为”对话框中输入的文件名实例化StreamWriter对象

                    StreamWriter sw = new StreamWriter(savefiledialog.FileName, true);

                    //向创建的文件中写入内容

                    sw.WriteLine(textBox1.Text);

                    //关闭当前文件写入流

                    sw.Close();

                    textBox1.Text = string.Empty;

                }

            }

 

        }

    }

}

 

原创粉丝点击