简体   繁体   中英

Display lins from text.file to array c#

I am trying to read lines from txt file into array and display it into a text box. Here is my code:

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack != true)
    {
        blogPostTextBox.Text ="";
        string blogFilePath = Server.MapPath("~") + "/Blogs.txt";
        string[] blogMessageArray = File.ReadAllLines(blogFilePath);

        // this for loop code does not work..I think.. 
        for (int i = 0; i < blogMessageArray.Length; i++)
        {
            string[] fileLineArray = blogMessageArray[i].Split(' ');
            blogPostTextBox.Text = blogMessageArray[i] + System.Environment.New Line.ToString();
        }   
    }
}

My text file contains several line and I am trying to split each line to array and display all lines into a text box using a for loop or while loop.

UPDATE:

For ASP.Net

var items =File.ReadAllLines(blogFilePath).SelectMany(line => line.Split()).Where(x=>!string.IsNullOrEmpty(x));
blogPostTextBox.Text=string.Join(Environment.NewLine, items)

and as a side note, it is better to use Path.Combine when you build path from multiple strings

string blogFilePath = Path.Combine( Server.MapPath("~") , "Blogs.txt");

also if (IsPostBack != true) is valid but you can do as

if (!IsPostBack)

Winform

If the Multiline property of the text box control is set to true, you can use TextBoxBase.Lines Property

blogPostTextBox.Lines =File.ReadAllLines(blogFilePath);

if you need to split each line and set as textbox text then

blogPostTextBox.Lines = File.ReadAllLines(blogFilePath).SelectMany(line => line.Split()).ToArray();

You have to set TextMode="MultiLine" in your TextBox (default value is SingleLine ), then you could build the text with Linq in this way:

var allLinesText = blogMessageArray
     .SelectMany(line => line.Split().Select(word => word.Trim()))
     .Where(word => !string.IsNullOrEmpty(word));
blogPostTextBox.Text = string.Join(Environment.NewLine, allLinesText);

You need to append each line to the textbox. What you are doing above is overwriting the contents of the textbox with each new line.

string[] blogMessageArray = File.ReadAllLines("");
blogPostTextBox.Text = "";
foreach (string message in blogMessageArray)
{
    blogPostTextBox.Text += message + Environment.NewLine;
}

Although rather than read in all the lines, and then write out all the lines, why don't you just write out all the text to the textbox?

blogPostTextBox.Text = File.ReadAllText();

Though you could do this in a loop, you really don't need to (or want to)

Replace your loop with with built in dot net methods that actually do what your need.

See String.Join()

public static string Join(
    string separator,
    params string[] value
)

This will combine all of the elements of blogMessageArray with your specified separator ('\\n' in your case, HTML does not need "\\r\\n")

Then just assign this to the property blogPostTextBox.Tex

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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