Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm using a really simple c# ini file class.

But when I try to read from my ini file, I always get the default string value.

The ini class:

C#
using System;
using System.Runtime.InteropServices;
using System.Text;
 
namespace SimChecklist
{
    class IniFile
    {
        [DllImport("kernel32")] private static extern long WritePrivateProfileString(string section,
          string key, string value, string iniFilePath);
        [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section,
          string key, string defValue, StringBuilder retValue, int size, string iniFilePath);
 
        private const int valueLength = 255; // Maximum length of value
        private String iniPath; // The path of our .ini file
 
        public IniFile(String iniFilePath)
        {
            iniPath = iniFilePath;
        }
 
        public void WriteString(String section, String key, String value)
        {
            WritePrivateProfileString(section, key, value, iniPath);
        }
        public void WriteInt(String section, String key, int value)
        {
            WriteString(section, key, value.ToString());
        }
        public void WriteDouble(String section, String key, Double value)
        {
            WriteString(section, key, value.ToString());
        }
 
        public String ReadString(String section, String key, String defValue)
        {
            StringBuilder tmp = new StringBuilder(valueLength);
            int i = GetPrivateProfileString(section, key, defValue, tmp, valueLength, iniPath);
 
            return tmp.ToString();
        }
        public int ReadInt(String section, String key, int defValue)
        {
            return Int32.Parse(ReadString(section, key, defValue.ToString()));
        }
        public Double ReadDouble(String section, String key, Double defValue)
        {
            return Double.Parse(ReadString(section, key, defValue.ToString()));
        }
    }
}


My use in my mainform:

C#
IniFile ini = new IniFile("C:\\Checklist.ini");
string tempString = ini.ReadString("simTheme", "simType", "aaaa");


My ini file:

C#
[simTheme]
simType="FS2004 A Century of Flight"
simTypeMaxLen=32

So, tempString always == "aaaa".

Thank you for any help.
Posted
Comments
George Jonsson 27-Jun-14 21:30pm    
Your code works.
Are you sure you have the correct path and file name?
If the ini-file is missing the method returns the default value.
George Jonsson 27-Jun-14 21:38pm    
But I agree with the others. Unless you have legacy reasons to use an ini-file, you should XML instead.

This is how I implemented GetPrivateProfileString for someone in 2003. I've never used it; I always use XML for config files.

[
    System.Runtime.InteropServices.DllImportAttribute
    (
        "Kernel32"
    ,
        SetLastError=true
    ,
        EntryPoint="GetPrivateProfileString"
    )
]
private unsafe static extern uint
API_GetPrivateProfileString
(
    string lpAppName
,
    string lpKeyName
,
    string lpDefault
,
    byte*  lpReturnedString
,
    int    nSize
,
    string lpFileName
) ;

public unsafe static int
GetPrivateProfileString
(
    string     lpAppName
,
    string     lpKeyName
,
    string     lpDefault
,
    out string lpReturnedString
,
    int        nSize
,
    string     lpFileName
)
{
    int    result ;
    byte[] temp   = new byte [ nSize ] ;

    fixed ( byte* ptemp = temp )
    {
        result = (int) API_GetPrivateProfileString
        (
            lpAppName
        ,
            lpKeyName
        ,
            lpDefault
        ,
            ptemp
        ,
            nSize
        ,
            lpFileName
        ) ;
    }

    lpReturnedString = System.Text.Encoding.Unicode.GetString ( temp ).Substring ( 0 , result ) ;

    return ( result ) ;
}
 
Share this answer
 
This approach is way to complex, even though you use "official" Microsoft API.

It would be much better to switch to XML (or, JSON, maybe?), whatever is more convenient. But if you really, really need to use legacy INI files, it will be million times simpler and more reliable to do this: create a dictionary of entries. Read the file line by line and read all sections, key-value pairs and put it in the dictionary. It could be nested dictionary of sections, providing a section by the key (section name) and the section could be the nested dictionary providing a value by a key. This way, you may have identical keys in different sections. This way, you will get 3-level INI structure: section/key/value.

This solution can be implemented in minutes and will work immediately.

—SA
 
Share this answer
 
Comments
rfresh 27-Jun-14 23:50pm    
I got it working. I have a long path and was one char off.

Thanks for the help and comments everyone.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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