从文件里逐行读取数据

编程入门 行业动态 更新时间:2024-10-26 00:27:28
本文介绍了从文件里逐行读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前正在打开一个文件,并显示在一个消息框。

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 }

更多推荐

从文件里逐行读取数据

本文发布于:2023-07-06 07:54:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047556.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   数据   里逐行

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!