Question Saving Certain Text Lines to String

Haxaro

Well-known member
Joined
Mar 13, 2009
Messages
105
Programming Experience
1-3
How can i make a program that will read a text file (.txt) and save each seperate line as a variable?

e.g.

Text File:
Hello
My
Name
Is
Haxaro

would be saved in the program as variables as:
A = Hello
B = My
C = Name
D = Is
E = Haxaro

but if i only want
Hello, Name and Haxaro, then i can just call upon A,C and D?
Thanks!
 
If you know exactly how many lines are in the file then you simply declare that many variables, open a StreamReader and call ReadLine that many times.

If you don't know how many lines are in the file then you can't declare individual variables because you don't know how many to declare. In that case your best bet is to call File.ReadAllLines, which returns a String array. You can then get the individual lines from the elements of the array.
 
Back
Top