Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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 I'm not sure how to use it. I've founds peices of information but nothing I can use together.

 

Any help out there?

 

Thanks,

  • Leaders
Posted

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)

[/code]

I haven't tested it and some exception handling would be a very good idea, but that's the jist of it.

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...