Realization of C Using Windows API to Read and Write INI File

  • 2021-07-18 08:48:46
  • OfStack

This paper describes the method of C # 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, automatically create INI
If GetLastError: 5 checks whether IniPath added a filename at creation time. ini


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
namespace NameSpace
{
 /// <summary>
 ///  Utilization Windows API Read and write INI Documents 
 ///  When writing, if there is no INI File, automatically created INI
 ///  If at the time of creation, GetLastError:5  Check IniPath Is a file name added .ini
 /// </summary>
 public class INI
 {
  // Declaration kernel32.dll 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>
  ///  Construction method 
  /// </summary>
  /// <param name="INIPath">INI Absolute path of the file, no slash is needed after it </param>
  /// <param name="INIFileName">INI The file name does not need a slash, it needs .ini</param>
  public INI(string INIPath,string INIFileName)
  {
   Console.WriteLine("INI Object building");
   IniPath = INIPath + "\\" + INIFileName;
   Console.WriteLine("INIFilePath :" + IniPath);
  }
  /// <summary>
  ///  Write INI Documents 
  /// </summary>
  /// <param name="Section">Section</param>
  /// <param name="Key">Key</param>
  /// <param name="Value">Value</param>
  public void IniWriteValue(string Section, string Key, string Value)
  {
   Console.WriteLine("---IniWriteValue---");
   Console.WriteLine("Section :" + Section);
   Console.WriteLine("Key :" + Key);
   Console.WriteLine("Value :" + Value);
   Console.WriteLine("IniPath :" + IniPath);
   UInt32 Snapshot = GetLastError();
   //
   WritePrivateProfileString(Section, Key, Value, IniPath);
   if (Snapshot != GetLastError())
   {
    Console.WriteLine("GetLastError :" + GetLastError());
   }
  }
  /// <summary>
  ///  Readout INI Documents 
  /// </summary>
  /// <param name="Section">Section</param>
  /// <param name="Key">Key</param>
  public string IniReadValue(string Section, string Key)
  {
   StringBuilder result = new StringBuilder(256);
   GetPrivateProfileString(Section, Key, null, result, 256, IniPath);
   return result.ToString();
  }
  public bool ExistINIFile()
  {
   return File.Exists(IniPath);
  }
  /// <summary>
  /// creat config file to application ini
  /// </summary>
  /// <param name="dnf_path"></param>
  public void CreateConfig(string IP)
  {
   Console.WriteLine("CreateConfig");
   Console.WriteLine("IP:" + IP);
   try
   {
    WriteConfigIP(IP);
    if (ExistINIFile())
    {
     Console.WriteLine(" Profile created successfully ");
    }
    else
    {
     Console.WriteLine(" Profile creation was unsuccessful ");
    }
   }
   catch (Exception err)
   {
    Console.WriteLine(" Error message :" + err.ToString());
   }
  }
  /// <summary>
  /// write config for ip information
  /// </summary>
  /// <param name="IP"></param>
  public void WriteConfigIP(string IP)
  {
   string Section = "Config";
   string Key = "IP";
   string Value = IP;
   try
   {
    IniWriteValue(Section, Key, Value);
   }
   catch (Exception err)
   {
    Console.WriteLine(" Error message :" + err.ToString());
   }
  }
  public string ReadConfigIP()
  {
   try
   {
    string Section = "Config";
    string result = IniReadValue(Section, "IP");
    Console.WriteLine("ReadConfigIP Result :" + result);
    return result;
   }
   catch (Exception err)
   {
    Console.WriteLine(" Error message :" + err.ToString());
    return "Read Error";
   }
  }
 }
}

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


Related articles: