简体   繁体   English

将.txt 文件读入列表 C#

[英]Reading .txt file into a List C#

I am new to C# coming from Python, and this piece is really confusing to me...我是来自 Python 的 C# 的新手,这篇文章真的让我很困惑......

I have a simple.txt file with the values:我有一个 simple.txt 文件,其中包含以下值:

Name1
Name2
Name3

I want to read those into a List, 1 item in list per row.我想将它们读入一个列表,每行列表中的 1 个项目。 Seems easy enough but I can't figure it out.似乎很容易,但我无法弄清楚。 What am I doing incorrectly?我做错了什么? Thanks谢谢

using System;
using System.Collections.Generic;

string LOG_PATH = "C:\\Users\\xyz\\source\\repos\\LoopPractice\\TextFile1.txt"
List<string> allLinesText = ReadAllLines(LOG_PATH).ToList();

I think, perhaps, you found the documentation to ReadAllLines but didn't include the full declaration.我想,也许您找到了ReadAllLines的文档,但没有包含完整的声明。

Try this:尝试这个:

List<string> allLinesText = System.IO.File.ReadAllLines(LOG_PATH).ToList();

ReadAllLines is a static method in the class System.IO.File . ReadAllLines是 class System.IO.File中的static方法。 If you want to simplify the above line you could add using System.IO;如果您想简化上述行,您可以using System.IO; to the top of your file, then your code would become:到文件的顶部,那么您的代码将变为:

List<string> allLinesText = File.ReadAllLines(LOG_PATH).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM