Windows System C Read and Write ini Configuration File Program Code Sample Sharing

  • 2021-09-16 07:48:26
  • OfStack

Recently, I came into contact with reading and writing INI configuration files. Although Microsoft recommended using registry instead of INI configuration files a long time ago, there is also a special. Net configuration file format on Visual Studio, but it seems that INI configuration files are pleasing to the eye. In fact, the XML format configuration file of Net is more powerful in function, and I also recommend you to use this type of configuration file to develop Net software. The reason why I use INI configuration file is nothing more than to taste a fresh and personal habit.

C # itself does not provide access to INI configuration files, but we can use WinAPI provided methods to deal with INI file read and write, the code is very simple! There are a lot of ready-made codes on the Internet, which are just for recording and sorting, so as to facilitate future use.

What is the composition of the INI configuration file?

The INI file is a text file consisting of sections (section). Under each section name with brackets, there are several keywords (key) and their corresponding values (Value), and these keywords (key) belong to the section (section) located on the keyword (key).


[Section]
Key1=Value1
Key2=Value2


The API functions GetPrivateProfileString () and WritePrivateProfileString () of Win32 in Windows system respectively realize the operation of reading and writing INI files
Therefore, you can write a simple class to read and write ini files to meet the configuration access requirements of a general application:

Example


class IniFile 
  { 
    /* 
    *  Declaration API Function  
    */ 
    public string iniPath; 
    [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); 
 
    /// <summary> 
    ///  Constructor  
    /// </summary> 
    /// <param name="iniPath">ini File path, default to current path default.ini</param> 
    public IniFile(string iniPath = "./default.ini") 
    { 
      this.iniPath = iniPath; 
    } 
 
    /// <summary> 
    ///  Write ini Documents  
    /// </summary> 
    /// <param name="Section">Section</param> 
    /// <param name="Key"> Key </param> 
    /// <param name="Value"> Value </param> 
    public void writeIni(string Section, string Key, string Value) 
    { 
      WritePrivateProfileString(Section, Key, Value, this.iniPath); 
    } 
 
    /// <summary> 
    ///  Write ini Documents, regardless section The default is placed in default Li  
    /// </summary> 
    /// <param name="Key"> Key </param> 
    /// <param name="Value"> Value </param> 
    public void writeIni(string Key, string Value) 
    { 
      WritePrivateProfileString("default", Key, Value, this.iniPath); 
    } 
 
    /// <summary> 
    ///  Read ini Documents  
    /// </summary> 
    /// <param name="Section">Section</param> 
    /// <param name="Key"> Key </param> 
    /// <returns> Value returned </returns> 
    public string readIni(string Section, string Key) 
    { 
      StringBuilder temp = new StringBuilder(256); 
      int i = GetPrivateProfileString(Section, Key, "", temp, 256, this.iniPath); 
      return temp.ToString(); 
    } 
 
    /// <summary> 
    ///  Read section , regardless section , default from the default Read in  
    /// </summary> 
    /// <param name="Key"> Key </param> 
    /// <returns> Return value </returns> 
    public string readIni(string Key) 
    { 
      return readIni("default", Key); 
    } 
 
    /// <summary> 
    ///  Query ini Does the file exist  
    /// </summary> 
    /// <returns> Does it exist </returns> 
    public bool existINIFile() 
    { 
      return File.Exists(iniPath); 
    } 
  } 

Call mode:


class Program 
{ 
  static void Main(string[] args) 
  { 
    IniFile iniFile = new IniFile("./hello.ini"); 
    iniFile.writeIni("section1", "key1", "value11"); 
    iniFile.writeIni("section1", "key2", "value12"); 
    iniFile.writeIni("section2", "key1", "value21"); 
    iniFile.writeIni("section2", "key2", "value22"); 
 
    iniFile.writeIni("key", "value"); 
 
    string str = iniFile.readIni("key"); 
    Console.WriteLine(str); 
    Console.ReadKey(); 
  } 
} 


Related articles: