Monday, March 26, 2012

Text files, reading,writing and saving

Hi,

I want a textbox on my site, you write in it and click save

the text is then saved as 'sample.txt'

then I also want that page to OPEN 'sample.txt' and read the contents into a textbox

I just want the simplest way of doing this

can someone point me at the code

thanks

Seethis tutorial which does what you ask.

yes, that answered it

thanks!


AHHH... looks like the right code however...

got a couple of textboxes and buttons

I pasted this code from the tutor into the code area of a button

System.IO.StreamReader StreamReader1=new System.IO.StreamReader(Server.MapPath("test.txt"));TextBox2.Text= StreamReader1.ReadToEnd();StreamReader1.Close();
it says: streamreader is a type in IO and cannot be used as an expression
it also says streamreader is not declared.
im using basic not C is that the problem?

Yes, the tutorial is in C#, not VB. For a VB tutorial trythis page.

yes the code example is written in C

however im using basic

visual web developer express

anyone know how to read write and save a file ...simply

it seems as though I can't use streamreader...there is no help on it in web developer express.


thanks for your help.Geeked

To read.. there's a great new addition to the System.IO namespace and that's "ReadAllText"

So you'd say

Text1.Text = System.IO.File.ReadAllText(Server.MapPath("~/sample.txt"))

and that's it!

For writing, the above links that everyone else is fine... 3 lines using the StringWriter class... and never ever forget to ".Dispose" the object otherwise it will lock the file


You've probably worked this out by now , but I wrote a little tutorial the other day that does exactly this, it over here if you're still intrestedwww.codeproject.com

0 comments:

Post a Comment