using System; using System.Text; using System.Runtime.InteropServices; using System.Collections.Specialized; namespace TBF.Tools { /// /// The class use the WinApi GetPrivateProfileSectionNames, /// GetPrivateProfileSection, GetPrivateProfileString, WritePrivateProfileString /// and present easy methods to work from a NET point of view. /// Usage: /// IniUtil ini = new IniUtil(@"C:\program files (x86)\myapp\myapp.ini"); /// string country = ini.GetValue("Carrier","Country","NoCountry"); /// public class IniUtil { [DllImport("kernel32.dll")] private static extern int GetPrivateProfileSectionNames(byte[] lpszReturnBuffer, int nSize, string lpFileName); [DllImport("kernel32.dll")] private static extern int GetPrivateProfileSection(string lpAppName, byte[] lpReturnedString, int nSize, string lpFileName); [DllImport("kernel32.dll")] private static extern int GetPrivateProfileString(string lpApplicationName, string lpKeyName, string lpDefault, byte[] lpReturnedString, int nSize, string lpFileName); [DllImport("kernel32.dll")] private static extern bool WritePrivateProfileString(string lpApplicationName, string lpKeyName, string lpString, string lpFileName); private const int VALUE_BUFFER = 511; private const int SECTION_BUFFER = (1024 * 16); private string m_sIniFile; /// /// .ctor with INI file name /// /// Fullpath to the INI file public IniUtil(string fileName) { m_sIniFile = fileName; } /// /// Set the value for a specific key in a section /// /// Section containing the key to write to /// Key to insert/update /// Value for the key /// True if OK public bool SetValue(string section, string key, string keyvalue) { return WritePrivateProfileString(section, key, keyvalue, m_sIniFile); } /// /// Gets the value of the specidied key in the specified section, /// If the key doesn't exists returns the default value /// /// Section containing the key to read from /// Required key /// Value to return in case the key is missing /// string value of the key or missing value public string GetValue(string section, string key, string ifMissing) { byte[] by = new byte[VALUE_BUFFER]; int n = GetPrivateProfileString(section, key, ifMissing, by, VALUE_BUFFER, m_sIniFile); string s = Encoding.ASCII.GetString(by); return s.Substring(0, n); } /// /// Returns the NameValueCollection for every key in the section /// /// Section name /// NameValueCollection with nake=Key and value=value public NameValueCollection GetSectionKeysvalues(string section) { NameValueCollection n = new NameValueCollection(); if(section.Length > 0) { byte[] by = new byte[SECTION_BUFFER]; int x = GetPrivateProfileSection(section, by, SECTION_BUFFER, m_sIniFile); if(x > 0) x--; string keysvalues = Encoding.ASCII.GetString(by, 0, x); string[] temp = keysvalues.Split('\0'); foreach(string s in temp) { string[] t = s.Split('='); n.Add(t[0], t[1]); } } return n; } /// /// Get the names of all sections in .INI /// /// string array with all the key names public string[] GetSectionNames() { byte[] by = new byte[SECTION_BUFFER]; int x = GetPrivateProfileSectionNames(by, SECTION_BUFFER, m_sIniFile); if(x > 0) x--; string keys = Encoding.ASCII.GetString(by, 0, x); return keys.Split('\0'); } } }