读取目录中的所有文件,并将结果与 数据网格进行比较

问题描述 投票:0回答:1

我需要读取给定目录中的文件列表(c:/ file / test)。

我只需要阅读第二行(总是这一行)。

该文件是.NT940所以我需要在记事本(或其他)中打开它。

我需要打开目录中的每个文件,读取第二行并将其存储在列表中。

 public List<FileInfo> Getextraitfile(string fileName)
        {
            List<FileInfo> listData = new List<FileInfo>();
            try
            {
                using (var reader = new StreamReader(fileName))
                {
                    while (!reader.EndOfStream)
                    {
                        string line = reader.ReadLine();

                        FileInfo fileInfo = new FileInfo();
                        fileInfo.DataLine = line;

                        listData.Add(fileInfo);
                    }
                }

                return listData;
            }
            catch (Exception ex)
            {
                Logger.logErreur("GetCsvFile : " + ex.Message + ex.StackTrace);
                return new List<FileInfo>();
            }
        }

我有这个得到nt90文件,但我完全迷路了,你能帮帮我吗?

c# list streamreader
1个回答
0
投票

欢迎来到SO。在将来的问题中,尝试更好地指定您需要的内容,包括示例和更多详细信息。我认为这样的事情会对你有所帮助:

    public List<string> GetLinesOfFile(string folderName)
    {
        FileInfo[] files = new DirectoryInfo(folderName).GetFiles("*.nt940");

        List<string> listYouNeed = new List<string>();

        foreach (FileInfo f in files)
        {
            string[] allLines = File.ReadAllLines(f.FullName);

            listYouNeed.Add(allLines[1]); //HERE YOU READ THE SECOND LINE OF THE FILE
        }

        return listYouNeed;
    }
© www.soinside.com 2019 - 2024. All rights reserved.