Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
while reading the section and key it's coming only section name
my file is like this.

[DefaultNames]
default1
default2

[DefaultType]
default1=1;
default2=2;

Please help me how to read and assign DefaultNames into combobox?

What I have tried:

C#
public class IniFile
   {
      public string path;
     // string Path = System.Environment.CurrentDirectory+"\\"+"ConfigFile.ini";
         
       [DllImport("kernel32")]
       private static extern long WritePrivateProfileString(string section,
           string key, string val, string filePath);
       [DllImport("kernel32")]
       private static extern int GetPrivateProfileString(string section,
                string key, string def, StringBuilder retVal,
           int size, string filePath);
      public IniFile(string Path)
       {
           path = Path;
       }
       
       public void IniWriteValue(string Section, string Key, string Value)
       {
           WritePrivateProfileString(Section, Key, Value, this.path);
       }
      
       public string IniReadValue(string Section, string Key)
       {
           StringBuilder temp = new StringBuilder(255);
           int i = GetPrivateProfileString(Section, Key, "", temp,
                                           255, this.path);
           return temp.ToString();

       }
    }

//In main Class
   public string ReadConfig(string section, string key)
        {
            string retVal = string.Empty;
            string bankname = string.Empty;
            string basePath = System.Environment.CurrentDirectory + "\\" + "Settings";
            IniFile ini = new IniFile(basePath + "\\" + "ConfigFile.ini");
            if (!Directory.Exists(basePath))
            {
                Directory.CreateDirectory(basePath);
                ini.IniWriteValue("DefaultNames", "default1", "2");
                ini.IniWriteValue("DefaultNames", "default2", "1");
                
            }
            retVal = ini.IniReadValue(section, key);
            return retVal;
        }
Posted
Updated 8-May-16 23:34pm
v2
Comments
Karthik_Mahalingam 9-May-16 3:33am    
Patrice T 9-May-16 4:52am    
Don't Repost, Improve first question instead.
Use Improve question to update your question.

The answer is don't use those Windows 16 bit historic legacy junk calls. See the note on the link in the answer above.
MSDN => This function is provided only for compatibility with 16-bit versions of Windows. Applications should store initialization information in the registry.

That is the reason you have to import the DLL headers and play around with marshalling the string data and they don't provide access to this stuff. At a guess the text will also be in ASCII codepage 850 mode (its based on old DOS) and you might get caught outwith stringBuilder in some country languages unless you set the encoding. I really don't know why you would want to do this out of a C# program ever and it's more dangerous than just directly accessing the files if it really has to be there.

The correct solution is to put the stuff into the registry like 32/64 bit programs are supposed to. If you really do want to store in config files just use the normal read write API calls or a file stream into a stock standard text file.

INI files are deprecated in favor of the registry for about a dozen good reasons and modern code shouldn't be using them.
 
Share this answer
 
v21
You need also the GetPrivateProfileSection function (Windows)[^] in order to retrieve key values.
 
Share this answer
 
unfortunately there is no builtin mechanism for reading them (Ini files). Here is code project article which reads your INI file, see An INI file handling class using C#[^]
secondly, there are lot of limitation of INI file you can see here Why are INI files deprecated in favor of the registry? | The Old New Thing[^], see if it should break your assumptions.
 
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