SoFunction
Updated on 2025-03-07

C# implements a method of reading and writing INI files using Windows API

This article describes the C# implementation method of using Windows API to read and write INI files. Share it for your reference. The details are as follows:

When writing, if there is no INI file, INI will be created automatically.
If at creation time GetLastError:5 Check if IniPath has added file name.ini

using System;
using ;
using ;
using ;
using ;
namespace NameSpace
{
 /// <summary>
 /// Use Windows API to read and write INI files /// When writing, if there is no INI file, INI will be created automatically /// If at creation time, GetLastError:5 Check whether IniPath has added file name.ini /// </summary>
 public class INI
 {
  //Declare the function  [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);
  //
  [DllImport("kernel32")]
  public static extern uint GetLastError();
  string IniPath = null;
  /// <summary>
  /// Construct method  /// </summary>
  /// <param name="INIPath">Absolute path to the INI file, no slash is needed afterwards</param>  /// <param name="INIFileName">The slash is not required when using the INI file name, .ini</param>  public INI(string INIPath,string INIFileName)
  {
   ("INI Object building");
   IniPath = INIPath + "\\" + INIFileName;
   ("INIFilePath :" + IniPath);
  }
  /// &lt;summary&gt;
  /// Write to INI file  /// &lt;/summary&gt;
  /// &lt;param name="Section"&gt;Section&lt;/param&gt;
  /// &lt;param name="Key"&gt;Key&lt;/param&gt;
  /// &lt;param name="Value"&gt;Value&lt;/param&gt;
  public void IniWriteValue(string Section, string Key, string Value)
  {
   ("---IniWriteValue---");
   ("Section :" + Section);
   ("Key :" + Key);
   ("Value :" + Value);
   ("IniPath :" + IniPath);
   UInt32 Snapshot = GetLastError();
   //
   WritePrivateProfileString(Section, Key, Value, IniPath);
   if (Snapshot != GetLastError())
   {
    ("GetLastError :" + GetLastError());
   }
  }
  /// &lt;summary&gt;
  /// Read out the INI file  /// &lt;/summary&gt;
  /// &lt;param name="Section"&gt;Section&lt;/param&gt;
  /// &lt;param name="Key"&gt;Key&lt;/param&gt;
  public string IniReadValue(string Section, string Key)
  {
   StringBuilder result = new StringBuilder(256);
   GetPrivateProfileString(Section, Key, null, result, 256, IniPath);
   return ();
  }
  public bool ExistINIFile()
  {
   return (IniPath);
  }
  /// &lt;summary&gt;
  /// creat config file to application ini
  /// &lt;/summary&gt;
  /// &lt;param name="dnf_path"&gt;&lt;/param&gt;
  public void CreateConfig(string IP)
  {
   ("CreateConfig");
   ("IP:" + IP);
   try
   {
    WriteConfigIP(IP);
    if (ExistINIFile())
    {
     ("Configuration file creation successfully");
    }
    else
    {
     ("Configuration file creation failed");
    }
   }
   catch (Exception err)
   {
    ("Error message:" + ());
   }
  }
  /// &lt;summary&gt;
  /// write config for ip information
  /// &lt;/summary&gt;
  /// &lt;param name="IP"&gt;&lt;/param&gt;
  public void WriteConfigIP(string IP)
  {
   string Section = "Config";
   string Key = "IP";
   string Value = IP;
   try
   {
    IniWriteValue(Section, Key, Value);
   }
   catch (Exception err)
   {
    ("Error message:" + ());
   }
  }
  public string ReadConfigIP()
  {
   try
   {
    string Section = "Config";
    string result = IniReadValue(Section, "IP");
    ("ReadConfigIP Result :" + result);
    return result;
   }
   catch (Exception err)
   {
    ("Error message:" + ());
    return "Read Error";
   }
  }
 }
}

I hope this article will be helpful to everyone's C# programming.