如何在C#中一行一行地读取? [英] How to read one line by one line in C#?

查看:348
本文介绍了如何在C#中一行一行地读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我的文件中包含诸如
之类的数据 002
潘妮
F
09876789
pkah123
2012年1月6日,星期五
2012年2月1日,星期三
金边

0012
asdf
F
asfd
asdf
2012年1月6日,星期五
2012年1月6日,星期五
asdf
asdf

然后,我想阅读它以放入列表视图,例如
ID名称性别地址电话护照StartDate EndDate描述
002 Phanny F金边09876889 pkah123 Friday .. wendesday .. good

然后,当它读完此内容时,当看到ID时,它将进入新行.

我只能在下面的一栏中进行操作
002PhannyF09876789pkah1232012年1月6日星期五2012年2月1日星期三金边Penhgood0012asdfFasfdasdf 2012年1月6日星期五2012年1月6日星期五asdfasdf

此结果仅在一行和一列中显示.
我的代码在这里:

Dear all,

I have a problem that in my files have data such as
002
Phanny
F
09876789
pkah123
Friday, January 06, 2012
Wednesday, February 01, 2012
Phnom Penh
good
0012
asdf
F
asfd
asdf
Friday, January 06, 2012
Friday, January 06, 2012
asdf
asdf

Then I want to read it to put in the list view such as
ID Name Gender Address Phone Passport StartDate EndDate Description
002 Phanny F Phnom Penh 09876889 pkah123 Friday.. wendesday.. good

Then when It read this finish, when it see the ID, it will enter the new line.

I can do it only in one column as below
002PhannyF09876789pkah123Friday, January 06, 2012Wednesday, February 01, 2012Phnom Penhgood0012asdfFasfdasdfFriday, January 06, 2012Friday, January 06, 2012asdfasdf

this result only in one line and in one column.
My code here:

string file_name = "D:\\AEU\\C# Programming\\TurismMangementSystem\\TurismMangementSystem\\test.txt";
           string textLine = "";
           System.IO.StreamReader objReader;
           objReader = new System.IO.StreamReader(file_name);
           do
           {
               //textLine = textLine + objReader.ReadLine() + "\r\n";

               textLine = objReader.ReadLine();
           } while (objReader.Peek() != -1);

           lis.SubItems.Add(textLine);
           listView1.Items.Add(lis);


请帮帮我.......

非常感谢.


Please help me.......

Thank you so much.

推荐答案

尝试一下并投票:


Try this and vote:


int counter = 0;
string line;

// Read the file and display it line by line.
System.IO.StreamReader file =
   new System.IO.StreamReader("c:\\test.txt");
while((line = file.ReadLine()) != null)
{
   Console.WriteLine (line);
   counter++;
}

file.Close();

// Suspend the screen.
Console.ReadLine();


好像您需要读取9行才能在此处进行记录,这可能对您有所帮助

it looks like you need to read 9 lines to make a record there for this might help

do
int linecount=1          
do{
//textLine = textLine + objReader.ReadLine() + "\r\n";
  if(linecount % 9 != 0)
  {
       textLine+= objReader.ReadLine();
       linecount++;
  }
  else
  {
    lis.SubItems.Add(textLine);
    listView1.Items.Add(lis);
    linecount = 1;
    textLine = "";
  }
} while (objReader.Peek() != -1);


这篇关于如何在C#中一行一行地读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆