当前位置:网站首页>C [file operation] read TXT text by line

C [file operation] read TXT text by line

2022-04-23 17:58:00 Tomorrow is like noon

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)
		{
			
			// Method 1 : According to the line read txt Text 
			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";
			}	
            // Method 2 :
            string[] strArray=File.ReadAllLines(path);	
            for(int i=0;i<strArray.Length;i++)
            {
                textBox1.Text += strArray[i]+ "\r\n";
            }
		}
	}
}

版权声明
本文为[Tomorrow is like noon]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230546376339.html