类别:程序开发

日期:2022-06-03 浏览:2145 评论:0

本文实例讲述了C#逐行读取txt文件的方法,是C#程序设计中非常实用的技巧,分享给大家供大家参考.

具体方法如下:

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;
 
namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            
            //方法一:按行读取txt文本
            string path = @"G:\Desktop\QuartzSideA-film_1.txt";
            StreamReader sr = new StreamReader(path, Encoding.Default);
            String line;
            while ((line = sr.ReadLine()) != null)
            {
                textBox1.Text += line + "\r\n";
            }    
            //方法二:
            string[] strArray=File.ReadAllLines(path);    
            for(int i=0;i<strArray.Length;i++)
            {
                textBox1.Text += strArray[i]+ "\r\n";
            }
        }
    }
}


本文标题:C#按行读取txt文本
本文链接:https://vtzw.com/post/1014.html
作者授权:除特别说明外,本文由 零一 原创编译并授权 零一的世界 刊载发布。
版权声明:本文不使用任何协议授权,您可以任何形式自由转载或使用。
 您阅读本篇文章共花了: 

 可能感兴趣的文章

评论区

发表评论 / 取消回复

必填

选填

选填

◎欢迎讨论,请在这里发表您的看法及观点。