简体   繁体   English

在 C# 中逐行读取文本文件

[英]Read text file line by line in C#

I am trying to read a text file line by line inside a textbox, but it returns only the word "!MANAGERS" in the textbox.我试图在文本框中逐行读取文本文件,但它只返回文本框中的“!MANAGERS”一词。

My text file is:我的文本文件是:

!MANAGERS
NUMBERS = 6
ADMIN = 1
!INFORMATIONS
741852:PAULO MARCO:MANAGER:TEAM

My code to get the file is:我获取文件的代码是:

public static string GetFile () {
    string filepath = @"C:\Files\projectmanager.txt";
    StreamReader reader = new StreamReader (filepath);
    string lines = reader.ReadLine ();
    var list = new List<string> ();
    list.Add (lines);
    string[] liness = list.ToArray ();

    foreach (string line in liness) {
        return line;
    }

    return "ERROR";
}

My textbox code is:我的文本框代码是:

String filetext = ToolLibrary.FileSystem.GetFile();
textbox1.Text = filetext;

If this is an option for you, this can be done in one line provided you have set Multiline to true on your textbox properties.如果这对您来说是一个选项,则可以在一行中完成,前提是您已在文本框属性上将 Multiline 设置为 true。

在此处输入图片说明

textbox1.Lines = System.IO.File.ReadAllLines(@"C:\Files\projectmanager.txt");

You can simply write你可以简单地写

textbox1.Text = File.ReadAllText(filepath);

Also, set the Multiline property of the text box to true .此外,将文本框的Multiline属性设置为true (You can do so in the properties window.) (您可以在属性窗口中执行此操作。)

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

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