C# reading variables into static string from text file -


i have seen several posts giving examples of how read text files, , examples on how make string 'public' (static or const), haven't been able combine 2 inside 'function' in way making sense me.

i have text file called 'myconfig.txt'. in that, have 2 lines.

 mypathone=c:\testone  mypathtwo=c:\testtwo 

i want able read file when start form, making both mypathone , mypathtwo accessible anywhere inside form, using :

readconfig("myconfig.txt"); 

the way trying now, not working, :

public voice readconfig(string txtfile) {     using (streamreader sr = new streamresder(txtfile))     {         string line;         while ((line = sr.readline()) !=null)         {             var dict = file.readalllines(txtfile)                            .select(l => l.split(new[] { '=' }))                            .todictionary( s => s[0].trim(), s => s[1].trim());         }         public const string mypath1 = dic["mypathone"];         public const string mypath2 = dic["mypathtwo"];     } } 

the txt file never grow on 5 or 6 lines, , not stuck on using streamreader or dictionary.

as long can access path variables name anywhere, , doesn't add 400 lines of code or , ok doing whatever best, safest, fastest, easiest.

i have read many posts people data should stored in xml, figure part doesn't matter because reading file , getting variables part same either way. aside, rather able use plain txt file (end user) edit without having understand xml. (which means of course lots of checks blank lines, path exist, etc...i ok doing part, wanna part working first).

i have read different ways using readalllines array, , create new separate 'class' file (which don't understand yet..but working on it). want find 'stable' way this. (project using .net4 , linq way)

thanks!!

the code you've provided doesn't compile. instead, try this:

public string mypath1; public string mypath2;  public void readconfig(string txtfile) {     using (streamreader sr = new streamreader(txtfile))     {         // declare dictionary outside loop:         var dict = new dictionary<string, string>();          // (this loop reads every line until eof or first blank line.)         string line;         while (!string.isnullorempty((line = sr.readline())))         {             // split each line around '=':             var tmp = line.split(new[] { '=' },                                   stringsplitoptions.removeemptyentries);             // add key-value pair dictionary:             dict[tmp[0]] = dict[tmp[1]];         }          // assign values need:         mypath1 = dict["mypathone"];         mypath2 = dict["mypathtwo"];     } } 

to take account:

  1. you can't declare public fields methods.

  2. you can't initialize const fields @ run-time. instead provide constant value them @ compilation time.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -