text file and line breaks

Last post 11-10-2006 12:52 AM by jessjing. 7 replies.

Sort Posts:

  • text file and line breaks

    11-09-2006, 5:37 PM
    • Member
      165 point Member
    • Mitha
    • Member since 06-14-2006, 10:29 PM
    • Posts 42

    I have .txt file and want it to display in ASP.NET server textbox control.

    When I view the text file in Notepad it looks good. But when I put that into the textbox the line breaks are not preserved. It looks very odd.

    Is there a way to display the .txt file without loosing the line breaks ?

     

  • Re: text file and line breaks

    11-09-2006, 6:36 PM
    • Star
      12,116 point Star
    • shados
    • Member since 07-07-2006, 7:24 PM
    • Posts 2,202

    How are you reading the file? Is you're using readline, it will get rid of the line breaks...

  • Re: text file and line breaks

    11-09-2006, 8:19 PM
    Answer
    • Contributor
      4,382 point Contributor
    • jessjing
    • Member since 09-26-2006, 4:03 AM
    • Posts 872

    hi,

    here is my code, it works fine,refer to it

     protected void Button1_Click(object sender, EventArgs e)
        {
            string fileName = Server.MapPath("mynote.txt");

            StreamReader objStreamReader;
            objStreamReader = File.OpenText(fileName );

            string contents = objStreamReader.ReadToEnd();

            TextBox1.Text = contents;

            objStreamReader.Close();
        }

    hope it helps

  • Re: text file and line breaks

    11-09-2006, 8:29 PM
    • All-Star
      17,453 point All-Star
    • albertpascual
    • Member since 05-23-2003, 2:11 PM
    • Riverside, CA
    • Posts 3,474

    The above will work fine, if you are displaying in HTML you may want to add string sline = stream.ReadToEnd().Replace("\r","<BR>");

    So will be display in HTML in linebreaks

    Cheers
    Al
    My Blog
    MapStats.NET
    Please click on 'Mark as Answer' if this post answered your question!
  • Re: text file and line breaks

    11-09-2006, 11:12 PM
    • Member
      165 point Member
    • Mitha
    • Member since 06-14-2006, 10:29 PM
    • Posts 42
    jessjing:

    hi,

    here is my code, it works fine,refer to it

     protected void Button1_Click(object sender, EventArgs e)
        {
            string fileName = Server.MapPath("mynote.txt");

            StreamReader objStreamReader;
            objStreamReader = File.OpenText(fileName );

            string contents = objStreamReader.ReadToEnd();

            TextBox1.Text = contents;

            objStreamReader.Close();
        }

    hope it helps

    Thanks for the response. 

    I used the same method to read the text file but it won;t preserve the line breaks.

     

     

  • Re: text file and line breaks

    11-09-2006, 11:13 PM
    • Member
      165 point Member
    • Mitha
    • Member since 06-14-2006, 10:29 PM
    • Posts 42
    shados:

    How are you reading the file? Is you're using readline, it will get rid of the line breaks...

    I am using the same approch as jessjing. 

     

  • Re: text file and line breaks

    11-09-2006, 11:14 PM
    • Member
      165 point Member
    • Mitha
    • Member since 06-14-2006, 10:29 PM
    • Posts 42
    shados:

    How are you reading the file? Is you're using readline, it will get rid of the line breaks...

    I am using the same approach as jessjing. 

     

  • Re: text file and line breaks

    11-10-2006, 12:52 AM
    • Contributor
      4,382 point Contributor
    • jessjing
    • Member since 09-26-2006, 4:03 AM
    • Posts 872

    hi,

    That's weird, it works so well on my computer, and I think it should work well

Page 1 of 1 (8 items)