#region License /* MIT License Copyright © 2005 Rob Loach (http://www.robloach.net) All rights reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #endregion License #region Using Dirivitives using System; using System.Runtime.InteropServices; using System.IO; #endregion Using Dirivitives namespace IniFile { #region Class Documentation /// /// IniFile 0.2 - Represents an interface for writing and reading an INI file. /// /// By Rob Loach (http://www.robloach.net) /// The following is a basic example of the IniFile's use: /// /// // Create the INI file /// IniFile ini = new IniFile("window.ini"); /// /// // Set the values /// ini.Write("Window", "Title","The Title is here!"); /// ini.Write("Window", "Height", 480); /// ini.Write("Window", "Fullscreen", false); /// ini.Write("Window", "Speed", 4823.423143); /// /// // Get some values from the ini file /// int height = ini.GetInt("Window", "Height"); /// int width = ini.Get("Window", "Width", 640); // get 640 by default /// bool fullscreen = ini.GetBoolean("Window", "Fullscreen"); /// double speed = ini.GetDouble("Window", "Speed", 1.0); /// /// // Using the indexer gets and sets strings: /// Console.WriteLine("Title: " + ini["Window", "Title"] ); /// ini["Window", "Author"] = "Rob Loach"; /// /// #endregion Class Documentation public class IniFile { #region Private Fields /// /// The private field for the filename of the INI file. /// private string m_File; #endregion Private Fields #region Constructors /// /// Creates or loads an INI file from the given file path. /// /// The filepath of the INI file. public IniFile(string filename) { System.IO.FileInfo fileinfo = new System.IO.FileInfo(filename); m_File = fileinfo.FullName; } /// /// Creates or loads an INI file from the provided FileInfo. /// /// The FileInfo of the desired INI file. public IniFile(FileInfo file) { m_File = file.FullName; } /// /// Creates or loads an INI file named data.ini . /// public IniFile() : this("data.ini") { } #endregion Constructors #region Properties /// /// Gets and sets the file path that's associated with this INI file. /// public string File { get { return m_File; } set { m_File = value; } } #region Indexers /// /// Gets and sets the section/key value held within the current INI file as strings. /// public string this[string section, string key] { get { return GetString(section, key); } set { Write(section, key, value); } } /// /// Gets a value held within the INI file as a string. /// public string this[string section, string key, string defaultValue] { get { return Get(section, key, defaultValue); } } /// /// Gets a value held within the INI file as an integer. /// public int this[string section, string key, int defaultValue] { get { return Get(section, key, defaultValue); } } /// /// Gets a value held within the INI file as a double. /// public double this[string section, string key, double defaultValue] { get { return Get(section, key, defaultValue); } } /// /// Gets a value held within the INI file as a boolean. /// public bool this[string section, string key, bool defaultValue] { get { return Get(section, key, defaultValue); } } #endregion Indexers #endregion Properties #region Private DLL Imports [DllImport("kernel32", EntryPoint="GetPrivateProfileStringA")] private static extern int GetPrivateProfileString(string lpApplicationName, string lpKeyName, string lpDefault, System.Text.StringBuilder lpReturnedString, int nSize, string lpFileName); [DllImport("kernel32", EntryPoint="WritePrivateProfileStringA")] private static extern int WritePrivateProfileString(string lpApplicationName, string lpKeyName, string lpString, string lpFileName); [DllImport("kernel32", EntryPoint="GetPrivateProfileIntA")] private static extern int GetPrivateProfileInt(string lpApplicationName, string lpKeyName, int nDefault, string lpFileName); #endregion Private DLL Imports #region Get /// /// Gets a string from the INI file using the default value of "". /// /// The section of which the value is held. /// The key of the value. /// The value of the key underneth the section header. If the value doesn't exist, "" is returned. public string Get(string section, string key) { return Get(section, key, ""); } /// /// Gets a string value from the INI file. /// /// The section of which the value is held. /// The key of the value. /// The value to be returned if the key doesn't exist. /// The value of the key underneth the section header. If the value doesn't exist, the provided default value is returned. public string Get(string section, string key, string defaultValue) { // Returns a string from your INI file System.Text.StringBuilder objResult = new System.Text.StringBuilder(256); int charCount = GetPrivateProfileString(section, key, defaultValue, objResult, objResult.Capacity, m_File); if (charCount > 0) return objResult.ToString().Substring(0, charCount); else return defaultValue; } /// /// Gets an integer from the INI file. /// /// The section of which the value is held. /// The key of the value. /// The value to be returned if the key doesn't exist. /// The value of the key underneth the section header. If the value doesn't exist, the provided default value is returned. public int Get(string section, string key, int defaultValue) { return GetPrivateProfileInt(section, key, defaultValue, m_File); } /// /// Gets a boolean value from the INI file. /// /// The section of which the value is held. /// The key of the value. /// The value to be returned if the key doesn't exist. /// The value of the key underneth the section header. If the value doesn't exist, the provided default value is returned. public bool Get(string section, string key, bool defaultValue) { string val = Get(section, key, defaultValue.ToString()).ToLower().Trim(); if(val == "1") return true; if(val == "0") return false; if(val == bool.FalseString.ToLower()) return false; if(val == bool.TrueString.ToLower()) return true; return defaultValue; } /// /// Gets a double value from the INI file. /// /// The section of which the value is held. /// The key of the value. /// If the value doesn't exist, this value is to be returned. /// The value of the key underneth the section header. If the value doesn't exist, the provided default value is returned. public double Get(string section, string key, double defaultValue) { double val; if(double.TryParse(Get(section, key, "Failed"), System.Globalization.NumberStyles.Number, System.Globalization.NumberFormatInfo.CurrentInfo, out val)) return val; else return defaultValue; } /// /// Gets a string value from the INI file. /// /// The section of which the value is held. /// The key of the value. /// The value of the key underneth the section header. public string GetString(string section, string key) { return Get(section, key); } /// /// Gets a string value from the INI file. /// /// The section of which the value is held. /// The key of the value. /// The value to be returned if the key doesn't exist. /// The value of the key underneth the section header. If the value doesn't exist, the provided default value is returned. public string GetString(string section, string key, string defaultValue) { return Get(section, key, defaultValue); } /// /// Gets an integer value from the INI file using the default value of 0. /// /// The section of which the value is held. /// The key of the value. /// The value of the key underneth the section header. If the value doesn't exist, 0 is returned. public int GetInt(string section, string key) { return Get(section, key, 0); } /// /// Gets an integer value from the INI file. /// /// The section of which the value is held. /// The key of the value. /// If the value doesn't exist, this value is to be returned. /// The value of the key underneth the section header. If the value doesn't exist, the provided default value is returned. public int GetInt(string section, string key, int defaultValue) { return Get(section, key, defaultValue); } /// /// Gets a double value from the INI file. /// /// The section of which the value is held. /// The key of the value. /// If the value doesn't exist, this value is to be returned. /// The value of the key underneth the section header. If the value doesn't exist, the provided default value is returned. public double GetDouble(string section, string key, double defaultValue) { return Get(section, key, defaultValue); } /// /// Gets a double value from the INI file. /// /// The section of which the value is held. /// The key of the value. /// The value of the key underneth the section header. If the value doesn't exist, 0.0 is returned. public double GetDouble(string section, string key) { return Get(section, key, 0.0); } /// /// Gets a boolean from the INI file using the default value of false. /// /// The section of which the value is held. /// The key of the value. /// The value of the key underneth the section header. If the value doesn't exist, false is returned. public bool GetBoolean(string section, string key) { return Get(section, key, false); } /// /// Gets an boolean value from the INI file. /// /// The section of which the value is held. /// The key of the value. /// If the value doesn't exist, this value is to be returned. /// The value of the key underneth the section header. If the value doesn't exist, the provided default value is returned. public bool GetBoolean(string section, string key, bool defaultValue) { return Get(section, key, defaultValue); } #endregion Get #region Write /// /// Writes a string to the INI file. /// /// The section to write under. /// The key to write in. /// The value to write to the key. /// True if the value successfully written, false otherwise. public bool Write(string section, string key, string val) { return WritePrivateProfileString(section, key, val, m_File) != 0; } /// /// Writes an integer to the INI file. /// /// The section to write under. /// The key to write in. /// The value to write to the key. /// True if the value successfully written, false otherwise. public bool Write(string section, string key, int val) { return Write(section, key, val.ToString()); } /// /// Writes a boolean value to the INI file. /// /// The section to write under. /// The key to write in. /// The value to write to the key. /// True if the value successfully written, false otherwise. public bool Write(string section, string key, bool val) { return Write(section, key, val ? "1" : "0"); } /// /// Writes a double value to the INI file. /// /// The section to write under. /// The key to write in. /// The value to write to the key. /// True if the value successfully written, false otherwise. public bool Write(string section, string key, double val) { return Write(section, key, val.ToString()); } #endregion Write } }