本文介绍了从文件里逐行读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在打开一个文件,并显示在一个消息框。
I am currently open a file and show it on a message box.
private void button2_Click_1(object sender, EventArgs e) { //OpenFileDialog1.ShowDialog(); OpenFileDialog file = new OpenFileDialog(); file.FileName = ""; file.Title = "Open A Text document."; file.Filter = "(*.gc)|*.gc|(*.etf)|*.etf|(*.txt)|*.txt|(*.GC)|*.GC|(*.tap)|*.tap"; DialogResult result = file.ShowDialog(); if (result == DialogResult.OK) { System.IO.StreamReader OpenFile = new System.IO.StreamReader(file.FileName); textBox1.Text = OpenFile.ReadToEnd(); OpenFile.Close(); } if (file.FileName.Length > 0) { OpenFile(file.FileName); }
但我需要逐行读取文件中的行,并显示在一个消息框。有没有办法来改变这种code。
But I need to read the file line by line and show it on a message box. Is there a way to change this code.
推荐答案有一个的 File.ReadLines 方法,这样你就可以做到这一点返回一个IEnumerable
There is a File.ReadLines method that returns an IEnumerable so you can do:
foreach(var line in File.ReadLines("myfile.txt")){ //Do stuff with line }更多推荐
从文件里逐行读取数据
发布评论