CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2005
    Posts
    568

    reading all key in section in INI file

    How can i loop all the key value inside a section in an INI file?

  2. #2
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: reading all key in section in INI file

    How are you reading the INI file to begin with?

  3. #3
    Join Date
    Feb 2005
    Posts
    568

    Re: reading all key in section in INI file

    Quote Originally Posted by nelo
    How are you reading the INI file to begin with?
    i just want to read from certain section... i know there is a function GetPrivateProfileString(), but it need to specify the exact section and keyname... what about i wanna read out all key and values inside the section?

  4. #4
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: reading all key in section in INI file

    I may be wrong but seems to be a Windows API function and not available in the .NET Framework directly. At least not in .NET 2.0. If you want to take that approach there is also the GetPrivateProfileSection function that will give you all the key value pairs in a section. Are you planning to use platform invoke to access the native Windows API?

  5. #5
    Join Date
    Mar 2008
    Location
    Atlanta, GA
    Posts
    49

    Re: reading all key in section in INI file

    If you didn't have access to an INI parser I would write one that read the data into a:

    Dictionary<string header, Dictionary<string key, string value>>

    Then you can find whatever key value you needed very easily.

    As far as the parsing itself, that's really easy to do, in fact, it's probably already done on codeproject.com or somewhere similar.

  6. #6
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: reading all key in section in INI file

    Quote Originally Posted by opedog
    If you didn't have access to an INI parser I would write one that read the data into a:

    Dictionary<string header, Dictionary<string key, string value>>

    Then you can find whatever key value you needed very easily.

    As far as the parsing itself, that's really easy to do, in fact, it's probably already done on codeproject.com or somewhere similar.
    Very good point. This would probably be a good approach if you are likely to want the same functionality in other projects/development work. You will have to weigh up the two options and decide which is more flexible/easy for what you need. OpeDog's approach is really good if you will need to access other sections of the file.

  7. #7
    Join Date
    Feb 2005
    Posts
    568

    Re: reading all key in section in INI file

    i just wanna read out the key on the particular section of the ini files...
    what is the different between GetPrivateProfileSection and GetProfileSection?

  8. #8
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: reading all key in section in INI file

    Quote Originally Posted by lsy
    i just wanna read out the key on the particular section of the ini files...
    what is the different between GetPrivateProfileSection and GetProfileSection?
    GetProfileSection is used for specifically for the win.ini file. For other INI files you have to use GetPrivatProfileSection.

  9. #9
    Join Date
    Feb 2005
    Posts
    568

    Re: reading all key in section in INI file

    Quote Originally Posted by nelo
    GetProfileSection is used for specifically for the win.ini file. For other INI files you have to use GetPrivatProfileSection.
    If i don't specify the section(AppName) name but only specify the key... would it be able to search whole file and return me the value of the key?
    i try
    GetPrivateProfileString(null, sKey, "", temp, 255, @sINIFile);
    i read from MSDN is should be able to return...
    pleae advice...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured