visual studio - Save Variables from a .txt File on C# -
sorry i´m new c# , working visual studio. i´m trying make .txt variables later when need change example number o path can change on .txt file , don´t need open code , update.
for example have archive.txt
joe;rodriguez;c:\example\hello.txt;398663
and code:
class program { static void main(string[] args) { string[] split; streamreader sr = file.opentext(@"c:\folder\archive.txt"); string line; while ((line = sr.readline()) != null) { split = line.split(new char[] { ';' }); string name = split[0]; string lastname = split[1]; string path = split[2]; string num = split[3]; console.writeline("split 0 is: " + name); console.writeline("split 2 is: " + lastname); console.writeline("split 3 is: " + path); console.writeline("split 4 is: " + num); } } }
what does, each value saves on variable after can use on code. getting value after sign ";"
how can save variables if .txt file saved this:
at begining .txt dile in 1 line , separated ";" want program saves value aftes "= (space)" , not being line enter on each line.
is there way?
thanks in advanced.
your problem reading in line @ time , calling split
on string contained line.
what can instead separate out variable in text file line, assign contents of each line different variable in each iteration of loop.
here's example of i'm talking about. uses dictionary can iterate through variables.
try this:
dictionary<string, string> myvars = new dictionary<string, string>(); while ((line = sr.readline()) != null) { split = line.split(new char[] { '=' }); myvars.add(split[0], split[1]); } console.writeline("split 0 is: " + myvars["name"]); console.writeline("split 2 is: " + myvars["lastname"]); console.writeline("split 3 is: " + myvars["path"]); console.writeline("split 4 is: " + myvars["num"]); console.readkey();
a downside approach in order use non-string variables, you'll have convert them shouldn't hard.
this example requires txt file formatted this:
name=joe lastname=rodriguez path=c:\example\hello.txt num=398663
Comments
Post a Comment