Click here to Skip to main content
16,011,120 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a requirement to search for a value in a text file and replace it with a value entered by a user. The file is of the following format:
[Value1]
Key1=0
Key2=1
Key3=2

[Value2]
Key1=0
Key2=1
Key3=2

[Value3]
Key1=0
Key2=1
Key3=2

etc..

I need to be able to do a search on a value/key pair and edit the value associated with the key. For example I'd like to be able to call a fucntion something like:
C#
WriteParam("[Value1]","Key3","123")

This will generate
[Value1]
Key1=0
Key2=1
Key3=123

So far I have come up with this:
C#
private void WriteTest()
       {

           List<string> lines = new List<string>(File.ReadAllLines(@"D:\InitiatorVals.ini"));
           foreach(string line in lines)
           {
               if((string.Compare(line,"[Type1]"))==0)
               {
                  //Found the value how to locate the key and edit the associated value

               }
           }
       }

Normally I would persevere and figure it out for myself but I'm under time constraints. Therefore I would be very grateful for any and all assistance.

Thanks so much
Posted
Updated 6-Jul-14 23:56pm
v2

Hello

see the below link


An INI file handling class using C#[^]
 
Share this answer
 
Comments
conradsmith 8-Jul-14 4:04am    
Hi thanks for the quick response. I like this solution. However when I implemented it the IniWriteFile didn't work. I quick search on the internet and I found I have to edit the registry. I entered my file InitiatorVals.ini to HKEY_LOCAL_MACHINE\SOFTWARE \Microsoft\Windows NT\CurrentVersion\IniFileMapping but I don't know what values to add to this.
Try this:
C#
private void WriteParam(string section, string key, string value)
{
    List<string> lines = new List<string>(File.ReadAllLines(@"D:\InitiatorVals.ini"));
    int index = lines.IndexOf(section);
    if (index > -1)
    {
        bool valueChanged = false;
        while (!lines[++index].StartsWith("["))
        {
            if (lines[index].StartsWith(String.Concat(key, "=")))
            {
                lines[index] = String.Format("{0}={1}", key, value);
                valueChanged = true;
                break;
            }
        }
        if (!valueChanged)
        {
            // key not found, create it if you want
        }
    }
    else
    {
        // section not found, if you want, create a new one
    }

    File.WriteAllLines(@"D:\InitiatorVals.ini", lines);
}

At lines[++index], it is necessary that you add the ++ before the variable name, because that makes sure that the return value of the operation is the value after the variable is incremented.
 
Share this answer
 
v3
Comments
conradsmith 8-Jul-14 4:18am    
Thank you very much. That works perfectly :-)
Thomas Daniels 8-Jul-14 5:00am    
You're welcome!
Here is my little logic with out the full code. hope it may give you some more idea.

<pre lang="c#">
private void WriteTest()
{

List lines = new List(File.ReadAllLines(@"D:\InitiatorVals.ini"));
foreach(string line in lines)
{
if((string.Compare(line,"[Type1]"))==0)
{
//Here is the new changes
//store the position to say int x
//find next index of '['
//store it to y

//from x to y in loop 
//find the key with equal sign ex: 'Key2='
//store the index and count upto new line character 
//replace it
//

}
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900