C# 无法用文件内容填充列表 命名空间控制台应用程序1 { 班级计划 { 静态void Main(字符串[]参数) { 列表文件1=新列表(); WriteLine(“输入文件夹的路径”); 字符串路径1=Console.ReadLine(); string text=System.IO.File.ReadAllText(路径1); } } }

C# 无法用文件内容填充列表 命名空间控制台应用程序1 { 班级计划 { 静态void Main(字符串[]参数) { 列表文件1=新列表(); WriteLine(“输入文件夹的路径”); 字符串路径1=Console.ReadLine(); string text=System.IO.File.ReadAllText(路径1); } } },c#,C#,我正在尝试将文件(文本)的内容放入列表中。我试图在此网站上查找帮助,但找不到任何帮助。如果您想获得每行的列表,请使用文件。ReadAllLines namespace ConsoleApplication1 { class Program { static void Main(string[] args) { List<string> file1 = new List<string>();

我正在尝试将文件(文本)的内容放入列表中。我试图在此网站上查找帮助,但找不到任何帮助。

如果您想获得每行的列表,请使用
文件。ReadAllLines

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> file1 = new List<string>();
            Console.WriteLine("Enter the path to the folder");
            string path1 = Console.ReadLine();

            string text = System.IO.File.ReadAllText(path1);
        }
    }
}
List file1=新列表();
WriteLine(“输入文件夹的路径”);
字符串路径1=Console.ReadLine();
file1.AddRange(System.IO.File.ReadAllLines(path1));
foreach(文件1中的var行)
{
控制台写入线(行);
}

这应该可以正常工作

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

Console.WriteLine("Enter the path to the folder");
string path1 = Console.ReadLine();

file1.AddRange(System.IO.File.ReadAllLines(path1));

foreach(var line in file1)
{
    Console.WriteLine(line);
}
static void Main(字符串[]args)
{
列表文件1=新列表();
Write(“输入文件夹的路径:”);
字符串路径1=Console.ReadLine();
file1.AddRange(System.IO.File.ReadAllLines(path1));
foreach(文件1中的var行)
{
控制台写入线(行);
}
Console.ReadKey();
}

如果您喜欢lambda,也可以选择这个

    static void Main(string[] args)
    {
        List<string> file1 = new List<string>();

        Console.Write("Enter the path to the folder:");
        string path1 = Console.ReadLine();

        file1.AddRange(System.IO.File.ReadAllLines(path1));

        foreach(var line in file1)
        {
            Console.WriteLine(line);
        }
        Console.ReadKey();
    }
static void Main(字符串[]args)
{
列表文件1=新列表();
Write(“输入文件夹的路径:”);
字符串路径1=Console.ReadLine();
file1.AddRange(System.IO.File.ReadAllLines(path1));
file1.ForEach(line=>Console.WriteLine(line));
Console.ReadKey();
}

您能告诉我们您是如何将文本输入列表的吗?另外,请正确设置问题的格式,以便阅读。您的标题中有语法问题,今后请多努力撰写您的问题,否则人们会否决您的问题,您的问题不会得到必要的关注,您也不会得到适当的答案。谢谢,但我现在无法显示。我所做的是Console.WriteLine(“{0},file1”);我必须以不同的方式执行吗?您可以使用foreach循环迭代列表。我将编辑答案
    static void Main(string[] args)
    {
        List<string> file1 = new List<string>();

        Console.Write("Enter the path to the folder:");
        string path1 = Console.ReadLine();

        file1.AddRange(System.IO.File.ReadAllLines(path1));

        file1.ForEach(line => Console.WriteLine(line));
        Console.ReadKey();
    }