Hello
In VB6, I was able to save and load values as text file, it saves them like this form:
"V1","V2","V3"
I was using this code:
Dim A As String, B As String, Op
Op = FreeFile
A = "Value1": B = "Value2"
Open "D:\File.txt" For Output As Op
Write #Op, A, B
Close Op
How can I do it in VB2005
Thanks & Regards

Save\Load values as text file
Lagear
Couple of comments:
1. The VB6 upgrade tool does quite a good job (sometimes). You can use that if you want to know how something may be done. It also pointed out a couple of problems with your existing VB6 code: Op gets upgraded to an object: the origional was declared as a variant.
2. There are a Ton+1 ways to do things in .NET. You may want to think about what you are trying to do, and type that into help: you'll get quite a few hits.
Now, to your problem: depending on how you write the variables, you will need to read them back in the same way. If you write them on the same line, you will need to separate them (witha comma is a goodway), then when you read the LINE back, split the line on the comma. The first string in the split is the first variable, the second string is the second one.
Perhaps you can write them on different lines The first one is A and the second one is B Or, write lines such as:
A=TheAString
B=TheBString
Then parse the lines, splitting on the '=' and interpreting the two strings for each line.
Some things to bear in mind:
they are strings - you will need to take care in converting the strings to numbers (if that's what you eventually may do). Additionally, what will happen to your program if you get two zero length strings Strings which are not valid for the application no line at all Strings with commas in them (these are the things you will HAVE to take care of).
Writing more data Perhaps you could put your variables into a serializable class (the whole class can be written as XML or a binary file, or a base64 encoded binary stream).
If they are Settings, you could use the my.settings to save the values, and let .NET take care of it.
Flea#
Please look at the code and see what its doing. Its simply writing a concatenated string. If you want to write commas in there than add them in the string, likewise with quotation marks.
A & "," & B
To load the file.
You can use the My.Computer.Filesystem.ReadAllText
http://msdn2.microsoft.com/en-us/library/ks09bsaw.aspx
or you can check out the TextFieldParser
http://msdn2.microsoft.com/en-US/library/microsoft.visualbasic.fileio.textfieldparser.aspx
All depends on what you want to do .
There are example on using each of the methods to read in the text on a file and process them. Thats down to you to work out for your own applications needs.
Mhd. akram
If you are writing a program to read the files written by the VB6 application then Spotty has the correct/closest answer. Use the text field parser.
Or use the vb6 upgrade tool: it's on the Tools menu - 'Upgrade Visual Basic 6 Code...".
Shifty
Thanks
It is a good example.
Todd West
I will explain more.
I defined A="v1" and B="v2" and I saved them as a text file.
My.Computer.FileSystem.WriteAllText ("D:\File.txt",A & "," & B)
That means that text file contains "v1,v2", right
Now after saving, I want to get A and B variables from File.txt
According to File.txt content, A=v1 and B=v2, right
How can I attach A=v1 & B=v2 (from File.txt)
In VB6, I was using the code in the top of this page.
MizzouTiger
I was using VB6 and I'm a new user in VB2005.
VB6 was saving them with commas and it can recall them easily by using this codes:
Save:
Dim A As String, B As String, Sv
Sv = FreeFile
A = "v1"
B = "v2"
Open "D:\File.txt" For Output As Sv
Write #Sv, A, B
Close Sv
Recall:
Dim A As String, B As String, Op
Op = FreeFile
Open "D:\File.txt" For Input As Op
Input #Op, A, B
Close Op
After saving, we note that File.txt contains: (look at the pic)
http://www.onh1986.com/vb2.jpg
Is there way to do that in VB2005
R. van Poelgeest
The following code will do the trick in VB Express/2005 making use of the My.Computer.FileSystem.WriteAllText method
Dim A As String, B As String
Dim Content as string =A & B
My.Computer.FileSystem.WriteAllText ("D:\File.txt", Content, False)
Snkscore
Shortly,
Is there a way to save variables and recall them to/from a text file
Filip Stanek
mimino
OK
I knew how to save the values A="v1" & B="v2".
But how can I load the values separately
I need A value and B value which equal "v1","v2".
BobK_MN
Spotty gave you the correct answer; you have to read the file back into the string and then parse it by using the Split() method at all commas.
But I think the real question here is: Do you need the CSV (comma seperated values) file If you just want to save your values and recall them later, and you don't really care about the file format, then you should follow the other suggestion posted here and change formats. XML would probably be a great option. Binary serializaion is also quick and easy if you only need sequential access to your save file.
If you can use XML then I'd highly suggest trying it. You should be able to find plenty of examples of simple XML manipulation. If you have to have the CSV, then read in the file and parse it with Split().
If you're still unsure how to proceed, please give us a better idea of what you're doing with the saved data (just saving for later use, transfering to some other person or system, or whatever) and how you need to access it (read the whole thing like opening a word file, or accessing specific pieces as needed like a database).
Kaz
"But how can I load the values separately "
I see the words, but I still do not understand what you are asking. What does load the values separately mean
Can you descibe the sequences of events the you desire to have happen
pcmarley
Here are two subroutines that represent what you used to do in VB6:
Private Sub SaveDataToText() Dim a, b As StringDim writer As System.IO.StreamWriter = System.IO.File.CreateText("c:\file.txt")
a =
"V1"b = "V2" writer.Write(ControlChars.Quote)
writer.Write(a)
writer.Write(ControlChars.Quote)
writer.Write(",")
writer.Write(ControlChars.Quote)
writer.Write(b)
writer.Write(ControlChars.Quote)
writer.Close() End Sub
sbussinger
OK
How can I load them from the text file
It saves them as one word "v1v2" not "v1","v2". (I supposed A=v1 and B=v2)