从文本文件中获取变量

本文关键字:获取 变量 文件 文本 | 更新日期: 2023-09-27 18:08:57

我有一个文本文件,每项都在<>它们之间没有空格

我需要能够读取记录的总数,并将其分配给一个变量。

我还需要将一行中的每个项目分配给一个变量,并将该行的编号分配给一个变量。这是为了以后可以处理它们。

我已经搜索了互联网,但我似乎只是在兜圈子,任何帮助或来源的想法将不胜感激。

从文本文件中获取变量

说明不是很清楚,但我试了一下。

    using System;
    using System.Data;
    using System.IO;
    using System.Collections.Generic;
class test
{
    public Dictionary<int, String> readAndSortFile()
    {
        StreamReader sr = new StreamReader("file.txt");
        Dictionary<int, String> dic = new Dictionary<int, string>();
        int loop = 0;
        while (!sr.EndOfStream)
        {
            dic.Add(loop, sr.ReadLine());
            loop++;
        }
        sr.Close();
        return dic;
    }
}

你的结果应该是这样的:

[0] {[0, <a>]}  
[1] {[1, <b>]}
[2] {[2, <c>]}  

你可以得到这样的文件

string file = System.IO.Directory.GetFiles(HttpContext.Current.Server.MapPath(folderName), "*.txt");
FileInfo fi = new FileInfo(file);
// to read all content of this file you can use -      
File.ReadAllLines(fi.FullName)

之后,你可以使用Dictionary来保存你的数据,然后你可以稍后使用它。你可以在这里阅读- http://csharp.net-informations.com/collection/dictionary.htm