读取文本文件中的所有数据示例
//设计类说明
partial class Form1
    {
 
        // 必需的设计器变量。
        private System.ComponentModel.IContainer components = null;          // 清理所有正在使用的资源。
    
        // <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }//CodeGo.net/
            base.Dispose(disposing);
        }        #region Windows 窗体设计器生成的代码        // 设计器支持所需的方法 - 不要
        // 使用代码编辑器修改此方法的内容。
         private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
            this.label1 = new System.Windows.Forms.Label();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // button1
            this.button1.Location = new System.Drawing.Point(197, 7);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(42, 23);
            this.button1.TabIndex = 22;
            this.button1.Text = "选择";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // textBox1
            this.textBox1.Location = new System.Drawing.Point(60, 9);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(131, 21);
            this.textBox1.TabIndex = 21;
            // openFileDialog1
            this.openFileDialog1.FileName = "openFileDialog1";
            // label1
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(13, 12);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(41, 12);
            this.label1.TabIndex = 20;
            this.label1.Text = "文件:";
            // textBox2
            this.textBox2.Location = new System.Drawing.Point(14, 58);
            this.textBox2.Multiline = true;
            this.textBox2.Name = "textBox2";
            this.textBox2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.textBox2.Size = new System.Drawing.Size(225, 90);
            this.textBox2.TabIndex = 24;
            // label2
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(13, 40);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(65, 12);
            this.label2.TabIndex = 23;
            this.label2.Text = "文件内容:";
            // Form1
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(253, 154);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.label2);
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "读取文件中所有数据";
            this.ResumeLayout(false);
            this.PerformLayout();
        }
        #endregion
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.OpenFileDialog openFileDialog1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox textBox2;
        private System.Windows.Forms.Label label2;
}
//应用类实例
  public Form1()
        {
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                openFileDialog1.Filter = "文本文件(*.txt)|*.txt";
                openFileDialog1.ShowDialog();
                textBox1.Text = openFileDialog1.FileName;
                StreamReader SReader = new StreamReader(textBox1.Text, Encoding.Default);
                textBox2.Text = SReader.ReadToEnd();
            }
            catch { MessageBox.Show("请选择文件"); }
        }

解决方案 »

  1.   

    一个是在 Form1_Load执行,一个是在按钮点击执行,以下是读取TXT代码类public string txtRead(string filepath, int rowbh)//读取指定的行
            {
                string str = "";
                try
                {
                    FileStream stream = new FileStream(filepath, FileMode.Open, FileAccess.Read);
                    StreamReader reader = new StreamReader(stream, System.Text.Encoding.Default);
                    reader.BaseStream.Seek(0L, SeekOrigin.Begin);
                    int num = 0;
                    for (string str2 = reader.ReadLine(); str2 != null; str2 = reader.ReadLine())
                    {
                        num++;
                        if (num == rowbh)
                        {
                            str = str2;
                            break;
                        }
                        
                    }
                    reader.Close();
                    stream.Close();
                }
                catch (Exception exception)
                {
                    string text = exception.ToString();
                    if (exception.InnerException != null)
                    {
                        text = text + exception.InnerException.ToString();
                    }
                    if (exception.StackTrace != null)
                    {
                        text = text + exception.StackTrace.ToString();
                    }
                    MessageBox.Show(text);
                }
                return str;
            }