0

I found some code that works perfectly with editing a certain section of an ini file, but it only picks up the first instance of the line that I want to edit. I know I can probably just manually input all the indexes in the code from where I want to start the editing, but knowing how things can change over time there might be changes to the ini file and then the indexes will change as well. Can someone shed some light to this issue?

const string FileName = "File.ini";
string file= File.ReadAllText(FileName);

const string Pattern = @"pattern = (?<Number>)";

Match match = Regex.Match(config, Pattern, RegexOptions.IgnoreCase);

if (match.Success)
{
    int index = match.Groups["Number"].Index;

    string newText= **********;
    file = file.Remove(index, 21);
    file = file.Insert(index, newText);

    File.WriteAllText(FileName, file);
}  
Hassan
  • 5,360
  • 2
  • 22
  • 35
KuyaBraye
  • 55
  • 1
  • 3
  • 8
  • 1
    Why `php` tag? Seems unnecessary. – Soner Gönül May 30 '14 at 07:33
  • 1
    Can you provide the content of the ini-file you are trying to edit? – Matthijs May 30 '14 at 07:36
  • 1
    Not got any better solution to hand but this is not a good solution as ini files have [Sections]. A fact this code ignores completely. – weston May 30 '14 at 07:43
  • 1
    possible duplicate of [Reading/writing an INI file](http://stackoverflow.com/questions/217902/reading-writing-an-ini-file) – Jonathon Reinhart May 30 '14 at 07:46
  • Sorry if I cannot give a proper description of what is in the ini file but this is typically the layout. [DB_1] ****** ****** pattern = ****** [DB_2] ****** ****** pattern = ****** This comment box doesn't allow me to add text on a next line but basically there is [DB_1] two lines of text under it, then a third line with the pattern, same goes for [DB_2] – KuyaBraye May 30 '14 at 08:00

2 Answers2

6

Easy way is to use WritePrivateProfileString and GetPrivateProfileString functions of Kernel32.dll for reading and writing into INI file.

Example:

For Writing to INI:

[DllImport("kernel32.dll", EntryPoint = "WritePrivateProfileString")]
public static extern long WriteValueA(string strSection, 
                                       string strKeyName, 
                                       string strValue, 
                                       string strFilePath);

Usage:

 WriteValueA("SectionToWrite", "KeyToWrite", "Value", @"D:\INIFile.ini");

For Reading from INI:

[DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileString")]
public static extern int GetKeyValueA(string strSection, 
                                      string strKeyName, 
                                      string strEmpty, 
                                      StringBuilder RetVal, 
                                      int nSize, 
                                      string strFilePath);

Usage:

StringBuilder temp = new StringBuilder(255);
int i = GetKeyValueA("TargetSection", "KeyToRead", string.Empty, temp, 255, @"D:\INIFile.ini");
string sValue = temp.ToString(); //desired value of the key
Hassan
  • 5,360
  • 2
  • 22
  • 35
1

Probably unrelated to question of why only one match, but note that in case of this regex:

const string Pattern = @"pattern = (?<Number>)";

Number group will contain an empty string. You probably want:

const string Pattern = @"pattern = (?<Number>\d+)";

See test results here

http://regex101.com/r/aT1aT0

user3613916
  • 232
  • 1
  • 10