reading and writing to a txt file

bjwade62

Well-known member
Joined
Oct 31, 2003
Messages
104
I have an existing txt file that I want to open, get the text (which is a number), add 1 to the number and save/close the txt file.

Sound like filestream but Im not sure how to use it. Ive founds peices of information but nothing I can use together.

Any help out there?

Thanks,
 
Assuming that you are using DotNet 2.0 or later, and the number is the only contents of the text file, you could do something along the lines of the following:
[Vb]

Dim FileName As String
Dim Lines() As String Contents of the files will be stored here

Lines = System.IO.File.ReadAllLines(FileName)
This assumes that the first line of the file contains a properly formatted number
Dim Value As Double = Double.Parse(Lines(0))
Increment the number
Value += 1
Store it back in the document
Lines(0) = Value.ToString()
Write the document back to the disk
System.IO.File.WriteAllLines(FileName, Lines)
[/VB]
I havent tested it and some exception handling would be a very good idea, but thats the jist of it.
 

Similar threads

Back
Top