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); } /// <summary> /// Write to INI file /// </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) { ("---IniWriteValue---"); ("Section :" + Section); ("Key :" + Key); ("Value :" + Value); ("IniPath :" + IniPath); UInt32 Snapshot = GetLastError(); // WritePrivateProfileString(Section, Key, Value, IniPath); if (Snapshot != GetLastError()) { ("GetLastError :" + GetLastError()); } } /// <summary> /// Read out the INI file /// </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 (); } public bool ExistINIFile() { return (IniPath); } /// <summary> /// creat config file to application ini /// </summary> /// <param name="dnf_path"></param> 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:" + ()); } } /// <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) { ("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.