读取文件中的每一行并将每一行放入一个字符串中 [英] Read each line in file and put each line into a string

查看:50
本文介绍了读取文件中的每一行并将每一行放入一个字符串中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要读入的文本文件,并将文件中的每一行放入其自己的字符串中.所以该文件将有 4 行:

I have a text file that I want to read in and put each line from the file into its own string. So the file will have 4 lines:

2017-01-20
05:59:30
+353879833382
971575 迈克尔

2017-01-20
05:59:30
+353879833382
971575 Michael

所以在代码中我需要读入文件并拆分每一行并将它们放入一个字符串中,即第一行将等于字符串日期,第二行将等于字符串时间等

So in the code I need to read in the file and split up each line and put them into a string i.e the first line will equal to string date, second line will equal to string time etc

代码:

public static void ParseTXTFile(string FileName, int CompanyID)
        {
            try
            {
                FileInfo file = new FileInfo(FileName);
                string Date;
                string Time;
                string Phone;
                string JobNo;
                string Name;

                using (CsvReader reader = new CsvReader(new StreamReader(FileName), false))
                {
                    while (reader.ReadNextRecord())
                    {


                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

如何读取文件的每一行并将其设置为字符串?

How do I read in each line of the file and set it to a string?

推荐答案

您可能需要考虑使用 File.ReadAllLines() 方法,它将把文件的每一行存储到一个数组中:

You may want to consider using the File.ReadAllLines() method which will store each line of your file into an array :

var lines = File.ReadAllLines(FileName);

然后,您可以根据需要通过索引访问每个属性:

You could then access each of your properties by their indices as needed :

string Date = lines[0];
string Time = lines[1];
string Phone = lines[2];
string JobNo = lines[3];
string Name = lines[4];

这篇关于读取文件中的每一行并将每一行放入一个字符串中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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