c# - How to read setting from INI-file -


i have text file installer.ini have row : fileexecutable=d:\location. want text after = , put in variable. after want use variable instead of d:\location in code.

    foreach (var txt in lin)     {         if (txt.contains("fileexecutable="))         {             var exefile = txt.split('=')[1];         }     }     // installer      process.startinfo.filename = @"d:\location";                            process.startinfo.arguments = @"-if c:\temp\installer.ini";     process.startinfo.windowstyle = processwindowstyle.hidden;     process.start();     process.waitforexit(); } 

i try put in variable exefile have in text file after fileexecutable=and use variable in row: process.startinfo.filename = @"d:\location";

the variable exefile declared inside foreach loop , therefore not useable after loop. if move outside loop can use after loop.

var exefile = string.empty; foreach (var txt in lin) {     if (txt.contains("fileexecutable="))     {         exefile = txt.split('=')[1];     } }  if (!string.isnullorempty(exefile)) {    // installer     process.startinfo.filename = exefile;                           process.startinfo.arguments = @"-if c:\temp\installer.ini";    process.startinfo.windowstyle = processwindowstyle.hidden;    process.start();    process.waitforexit(); } 

edit alternative

you can use regelar expression on whole ini-file skip looping through lines so:

using system; using system.text.regularexpressions;  public class program {     public static void main()     {          // dummy ini testing.         var ini = "test\nfileexecutable=d:\\location\ntest";          var regex = new regex("^fileexecutable=(.+)$", regexoptions.multiline);         var location = regex.match(ini).groups[1];          console.writeline(location);     } } 

edit yet alternative

after editing title of question came me might want read other settings ini well. if so, consider using ini-reader discussed here: reading/writing ini file


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -