How to read from a text file into a textbox?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: How to read from a text file into a textbox?

  1. #1
    Join Date
    Jul 2009
    Posts
    26

    How to read from a text file into a textbox?

    Is this correct way?


    StreamReader f = new StreamReader(new FileStream("c:\afile.txt", FileMode.Open, FileAccess.ReadWrite));
    char[] d = new char[????????];
    f.ReadBlock (d, 0, ??????); - or maybe f.Read()?
    textBox1.Text = new string(d);


    All MSDN/gooogle examples talk about lines... I want read ALL text, not only one line...
    How to learn the length of the text?

  2. #2
    Join Date
    Jul 2009
    Posts
    26

    Re: How to read from a text file into a textbox?

    StreamReader f = new StreamReader(new FileStream("c:\afile.txt", FileMode.Open, FileAccess.ReadWrite));
    textBox1.Text = f.ReadToEnd();

    =================

    OK! I have found...

    But how to write the text back?
    Must I create a new file and overwrite old file?
    Maybe I can use "FileAccess.ReadWrite" mode?

  3. #3
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: How to read from a text file into a textbox?

    Try this to read from a file into a text box
    Code:
    textBox1.Text = File.ReadAllText(@"C:\afile.txt");
    and this to write to a file from a text box
    Code:
    File.WriteAllText(@"C:\afile.txt", textBox1.Text);
    and this to find how much text is in the text box
    Code:
    Int32 length = textBox1.TextLength;
    and this to find how big the file is
    Code:
    FileInfo fileInfo = new FileInfo(@"C:\afile.txt");
    Int32 length = fileInfo.Length;
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: How to read from a text file into a textbox?

    One thing; I would use int instead of Int32. int is an aliased type, so you will never have the potential problems of type mismatches on different platforms (256-bit CPU's? Maybe someday! )
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  5. #5
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: How to read from a text file into a textbox?

    Ahh... thinking about it (and only thinking, not actually going to the effort of actually checking), I beleive the Length property may already be Int64.

    So on a 32 bit machine int is 32 bits and on a 64 bit machine int is 64 bits... Is that correct?
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: How to read from a text file into a textbox?

    Yes, 'int' is an alias to the native integer type on a given platform.
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  7. #7
    Join Date
    Jul 2009
    Posts
    26

    Re: How to read from a text file into a textbox?

    wow.. it is so easy! (must be.. I will try it now..).. so easy but so deeply hided..
    thank you a lot guys..
    I don not need file length in this case..
    I was looking it only for char[] d = new char[__?__]; f.ReadBlock (d, 0,__?__);
    Seems (to me) I must know it before file loading..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center