如何将文本文件读取到 C# 中的列表?

How to read a text file to a list in c#?

文本文件中的一行如下所示:

1056 Mark Supers Swagminator test32@gmail.com 3500

他们都必须进入他们自己的 属性 一个新对象,然后下一行将进入他们自己的对象。如何做到这一点?

这应该作为 'accounts' 的列表工作,这是帐户信息,名称等。

来自:Reading a text file word by word

using (var mappedFile1 = MemoryMappedFile.CreateFromFile(filePath))
    {
        using (Stream mmStream = mappedFile1.CreateViewStream())
        {
            using (StreamReader sr = new StreamReader(mmStream, ASCIIEncoding.ASCII))
            {
                while (!sr.EndOfStream)
                {
                    string line = sr.ReadLine();
                    string lineWords = line.Split(' ');
                    number = lineWords[0];
                    name = lineWords[1]; //and so on..
                }
            }  
        }
    }

我认为你可以很容易地用谷歌搜索。但这是一种简单易行的方法。表扬应该解释我在做什么。

    // Create Class that holds the Attributes for you
    // I am using auto properties here
    // if you don't know what a property is pls google it - you need to know that
    class AccountData
    {
        public int firstNumber { get; set; }

        public string firstString { get; set; }

        public string secondString { get; set; }

        public string thirdString { get; set; }

        public string mailAddress { get; set; }

        public int lastNumber { get; set; }

    }

    List<AccountData> Parser(string FileLocationWithName)
    {

        // FileLocationWithName is something like "C:\MyFolder\MyFile.txt"
        // If you want to write a backsplash (\) you need to write \
        // or you can use a @ bevore the string
        // without @: "C:\MyFolder\MyFile.txt"
        // with @: @"C:\MyFolder\MyFile.txt"

        // Create your list
        List<AccountData> resultList = new List<AccountData>();

        // Oben a new FileStream - a StreamReader is good
        using (StreamReader sr = new StreamReader(FileLocationWithName))
        {

            // Read the Whole file <=> sr is not at the end of the stream
            while (!sr.EndOfStream)
            {
                // read a line and split it into the strings
                string line = sr.ReadLine();

                var elementsOfLine = line.Split(' ');

                // create a new object of your accountData class and fill it with the string elements
                var tempElement = new AccountData();
                tempElement.firstNumber = int.Parse(elementsOfLine[0]);
                tempElement.firstString = elementsOfLine[1];
                tempElement.secondString = elementsOfLine[2];
                tempElement.thirdString = elementsOfLine[3];
                tempElement.mailAddress = elementsOfLine[4];
                tempElement.lastNumber = int.Parse(elementsOfLine[5]);

                resultList.Add(tempElement);
            }
        }

        // return your list
        return resultList;
    }
string line = "1056%Mark%Supers%Swagminator%test32@gmail.com%3500";
var propertyArray = line.Split('%');
foreach (string entry in propertyArray)
{
    Console.WriteLine(entry);
}

这将打印每个条目。您可以将每个条目用作数组中的一个简单对象。 propertyArray[0] 将是 1056

请注意尺寸。如果某人有 4 个名字,您的电子邮件地址将从 propertyArray[4] 移动到 propertyArray[5]