grandad
06-23-2004, 09:09 AM
Hi, could anyone point me in the direction of a tutorial on saving variables to a text file and then accessing this value at a later date. I know how to write a variable to a textfile using
oWrite.WriteLine(variablename)
oWrite.WriteLine(variablename2)
etc
but how do I read the value back into the variable from the textfile, and if there is more than one value in the text file how do I tell it which value to put into which variable...?
The full code i used for writing the variables to the file is...
----------------------
Dim oFile As System.IO.File
Dim oWrite As System.IO.StreamWriter
(on form load)
oWrite = oFile.CreateText("C:\Program Files\Folder\filename.txt")
(on button click)
oWrite.WriteLine(variable1)
oWrite.WriteLine(variable2)
oWrite.Close()
----------------------
I know how to open the file at a later date [using 'Dim oRead As System.IO.StreamReader' and 'oRead = oFile.OpenText("C:\Program Files\Folder\filename.txt")'], its everything after that I'm stuck on!
Many thanks in advance for any help you can give and sorry if I don't make enough sense for you to help!
okie20
06-23-2004, 09:15 AM
If you know what lines the variables are you can loop through streamreader.readline or .readtoend and use split(string,vbcrlf)
grandad
06-23-2004, 10:10 AM
If you know what lines the variables are you can loop through streamreader.readline or .readtoend and use split(string,vbcrlf)
thats great. What if I don't know the variable line though... Is there a way of writing the variable name AND the value it holds to the textfile, then reading this back and entering it into the correct variable.
say i have a variable called 'name' that holds the value "Ross", can i write both the variable name and value to the textfile ( in a format such as name = "Ross"), then read this back into the variable 'name'.
Many thanks again for your help.
Richard Crist
06-23-2004, 10:39 AM
What you are wanting to do makes sense, but there is most likely no way to convert a string of "name" into the actual variable called name. VB .NET may have a way to do that, but it would really surprise me. What you most likely will have to do is parse the file data for tokens (such as "name") and value ( "= value").
What would really be cool is to be able to cast a string as it's corresonding real variable name. The you could easily do what you are suggesting.
reboot
06-23-2004, 11:28 AM
Maybe I'm missing something, but what exactly is wrong with using a streamreader to read the variables back just like you wrote them?
Dim sr As StreamReader = New StreamReader("C:\Program Files\Folder\filename.txt")
variable1 = sr.ReadLine()
variable2 = sr.ReadLine()
sr.Close()
Richard Crist
06-23-2004, 11:42 AM
I think that he is saying that the data may not be stored in any particular order. This sounds corny at first, but I have had to develop systems to read token based data in random order. In my case I was dealing with a poorly implemented application sitting on top of an Oracle db. The code would spit out data in a file that was token based and I had to read it into my application and correctly interpret the data.
Now...grandad...I, like reboot and I'm sure others, am curious as to why, if you are writing the data, that the data will not be in any particular order? :)
reboot
06-23-2004, 11:44 AM
If that's the case, I have a class in the .Net code library to read and write application settings. Already written for you.
okie20
06-23-2004, 11:57 AM
This is kind of reiterating what has been said already, but...
So far you have,
writer.writeline(var1)
writer.writeline(var2)
etc.
Why not change this to
writer.writeline("Var1:" & var1)
writer.writeline("Var2:" & var2)
etc.
This way when you read the file from a streamreader and if the variables vary from different instances, then have a condition statement that can see this
dim N as integer = 0
while reader.readline
x = reader.readline
if instr(x,":") < x.length then
var(N) = x.substring(instr(x,":"))
N+=1
end if
loop
Basically, it is all string manipulation ... you just have to know what possibilities your file will have ... in my experience, reading values from a file is best obtained using delimeters. Hope this helps,
okie out