从文本文件中获取变量

时间:2015-08-19 15:42:00

标签: c# text-files

我有一个文本文件,每个项目都在<>他们被隔离,没有空间。

我需要能够读取记录总数并将其设置为变量。

此外,我需要将行中的每个项目与变量以及该行的数量对齐变量。这样他们可以在以后处理。

我已经在互联网上搜索,但我似乎只是在圈子里,任何帮助或来源的想法都会受到关注。

2 个答案:

答案 0 :(得分:0)

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

    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>]}  

答案 1 :(得分:0)

你可以得到这样的文件

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