逐字、逐行、直接读取文本文件

       最近,参考了一些资料,对读取文本文件的几种方法做了一些测试。但仍然有些Bug,例如,逐字逐行读取文本的过程不能中断。等解决这些问题后在更新内容吧。由于本人第一次写文章,不足处请^_^。

//ReadTxt_Set.Designer.cs代码

namespace TVDemo
{
    partial class ReadTxt_Set
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.tbTxt = new System.Windows.Forms.TextBox();
            this.btnSelect = new System.Windows.Forms.Button();
            this.btnReadByChar = new System.Windows.Forms.Button();
            this.btnReadByLine = new System.Windows.Forms.Button();
            this.bntReadByText = new System.Windows.Forms.Button();
            this.btnCancel = new System.Windows.Forms.Button();
            this.tbResult = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(12, 12);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(53, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "选取文本";
            //
            // tbTxt
            //
            this.tbTxt.Location = new System.Drawing.Point(71, 9);
            this.tbTxt.Name = "tbTxt";
            this.tbTxt.Size = new System.Drawing.Size(604, 21);
            this.tbTxt.TabIndex = 1;
            //
            // btnSelect
            //
            this.btnSelect.Location = new System.Drawing.Point(689, 9);
            this.btnSelect.Name = "btnSelect";
            this.btnSelect.Size = new System.Drawing.Size(38, 23);
            this.btnSelect.TabIndex = 2;
            this.btnSelect.Text = "...";
            this.btnSelect.UseVisualStyleBackColor = true;
            this.btnSelect.Click += new System.EventHandler(this.btnSelect_Click);
            //
            // btnReadByChar
            //
            this.btnReadByChar.Location = new System.Drawing.Point(14, 45);
            this.btnReadByChar.Name = "btnReadByChar";
            this.btnReadByChar.Size = new System.Drawing.Size(139, 23);
            this.btnReadByChar.TabIndex = 3;
            this.btnReadByChar.Text = "逐字读取文本";
            this.btnReadByChar.UseVisualStyleBackColor = true;
            this.btnReadByChar.Click += new System.EventHandler(this.btnReadByChar_Click);
            //
            // btnReadByLine
            //
            this.btnReadByLine.Location = new System.Drawing.Point(159, 45);
            this.btnReadByLine.Name = "btnReadByLine";
            this.btnReadByLine.Size = new System.Drawing.Size(142, 23);
            this.btnReadByLine.TabIndex = 5;
            this.btnReadByLine.Text = "逐行读取文本";
            this.btnReadByLine.UseVisualStyleBackColor = true;
            this.btnReadByLine.Click += new System.EventHandler(this.btnReadByLine_Click);
            //
            // bntReadByText
            //
            this.bntReadByText.Location = new System.Drawing.Point(308, 45);
            this.bntReadByText.Name = "bntReadByText";
            this.bntReadByText.Size = new System.Drawing.Size(139, 23);
            this.bntReadByText.TabIndex = 6;
            this.bntReadByText.Text = "直接读取文本";
            this.bntReadByText.UseVisualStyleBackColor = true;
            this.bntReadByText.Click += new System.EventHandler(this.bntReadByText_Click);
            //
            // btnCancel
            //
            this.btnCancel.Location = new System.Drawing.Point(465, 44);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(119, 23);
            this.btnCancel.TabIndex = 7;
            this.btnCancel.Text = "退出(返回)";
            this.btnCancel.UseVisualStyleBackColor = true;
            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
            //
            // tbResult
            //
            this.tbResult.Location = new System.Drawing.Point(14, 74);
            this.tbResult.Multiline = true;
            this.tbResult.Name = "tbResult";
            this.tbResult.ReadOnly = true;
            this.tbResult.ScrollBars = System.Windows.Forms.ScrollBars.Both;
            this.tbResult.Size = new System.Drawing.Size(713, 337);
            this.tbResult.TabIndex = 8;
            //
            // ReadTxt_Set
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(739, 423);
            this.Controls.Add(this.tbResult);
            this.Controls.Add(this.btnCancel);
            this.Controls.Add(this.bntReadByText);
            this.Controls.Add(this.btnReadByLine);
            this.Controls.Add(this.btnReadByChar);
            this.Controls.Add(this.btnSelect);
            this.Controls.Add(this.tbTxt);
            this.Controls.Add(this.label1);
            this.Name = "ReadTxt_Set";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "读取文本";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox tbTxt;
        private System.Windows.Forms.Button btnSelect;
        private System.Windows.Forms.Button btnReadByChar;
        private System.Windows.Forms.Button btnReadByLine;
        private System.Windows.Forms.Button bntReadByText;
        private System.Windows.Forms.Button btnCancel;
        private System.Windows.Forms.TextBox tbResult;

    }
}

 //ReadTxt_Set.cs代码

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;
using Microsoft.VisualBasic.FileIO;  //添加引用,引用.Net组件,引入Microsoft.VisualBasic组件

namespace TVDemo
{
    public partial class ReadTxt_Set : Form
    {
        public ReadTxt_Set()
        {
            InitializeComponent();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            OpenFileDialog sOpenFileDialog = new OpenFileDialog();
            sOpenFileDialog.Multiselect = false;
            sOpenFileDialog.RestoreDirectory = true;
            sOpenFileDialog.Title = "选择文本文件";
            sOpenFileDialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            if (sOpenFileDialog.ShowDialog() == DialogResult.OK)
            {
                this.tbTxt.Text = sOpenFileDialog.FileName;
            }
        }

        private void btnReadByChar_Click(object sender, EventArgs e)
        {
            if (CheckData() == true)
            {
                return;
            }
            try
            {
                frmStatus frm = new frmStatus(); 
                frm.ShowMessage("文件读取过程中,请勿退出,耐心等待...");
                StreamReader sStreamReader = FileSystem.OpenTextFileReader(this.tbTxt.Text.Trim(), Encoding.Default);
                this.tbResult.Clear();
                int myTxtInt = 0;
                while (myTxtInt != -1)
                {
                    this.tbResult.Text += (char)myTxtInt;
                    myTxtInt = sStreamReader.Read();
                    tbResult.Refresh();
                    System.Threading.Thread.Sleep(100);
                }
                frm.Close();
                MessageBox.Show("文件阅读完毕!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnReadByLine_Click(object sender, EventArgs e)
        {
            if (CheckData() == true)
            {
                return;
            }
            string myInputString = "";
            int rowCount = 0;
            this.tbResult.Clear();
            try
            {
                frmStatus frm = new frmStatus();
                frm.ShowMessage("文件读取过程中,请勿退出,耐心等待...");

                StreamReader sStreamReader = FileSystem.OpenTextFileReader(this.tbTxt.Text.Trim(), Encoding.Default);
                while (sStreamReader.Peek() != -1)
                {
                    this.tbResult.Text += myInputString;
                    myInputString = sStreamReader.ReadLine()+Environment.NewLine;
                    this.tbResult.Refresh();
                    rowCount += 1;
                    System.Threading.Thread.Sleep(100);
                }
                frm.Close();
                MessageBox.Show("文件阅读完毕!/n文本文件行数为:" + rowCount.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void bntReadByText_Click(object sender, EventArgs e)
        {
            if (CheckData() == true)
            {
                return;
            }
            try
            {
                frmStatus frm = new frmStatus();
                frm.ShowMessage("文件读取过程中,请勿退出,耐心等待...");

                this.tbResult.Clear();
                this.tbResult.Text = File.ReadAllText(this.tbTxt.Text.Trim(), Encoding.Default);
                frm.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private bool CheckData()
        {
            bool mBool = false;
            string mTmp = "";
            if (this.tbTxt.Text.Trim() == "")
            {
                mTmp += "请选择文本文件!/n";
                mBool = true;
            }
            if (File.Exists(this.tbTxt.Text.Trim()) == false)
            {
                mTmp += "文本文件不存在,请重新选择!";
                mBool = true;
            }
            if (mBool == true)
            {
                MessageBox.Show(mTmp, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return mBool;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

下面对frmStatus类加以说明。

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

namespace TVDemo
{
    public partial class frmStatus : Form
    {
        public frmStatus()
        {
            InitializeComponent();
        }

        public void ShowMessage(string message)
        {
            this.lbMsg.Text = message;  //界面只有一个Label控件,在InitializeComponent中将Dock设置为Fill
            this.ShowIcon = false;
            this.Show();
            Application.DoEvents();
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值