/// <summary> /// Gets the names of all keys under a specific section in the ini file. /// </summary> /// <param name="sectionName"> /// The name of the section to read key names from. /// </param> /// <returns>An array of key names.</returns> /// <remarks> /// The total length of all key names in the section must be /// less than 32KB in length. /// </remarks> /// <exception cref="ArgumentNullException"> /// <paramref name="sectionName"/> is a null reference (Nothing in VB) /// </exception> public string[] GetKeyNames(string argSectionName) { int len; string[] retval; if (string.IsNullOrEmpty(argSectionName)) { return(null); } //Allocate a buffer for the returned section names. IntPtr ptr = Marshal.AllocCoTaskMem(IniHandler.MaxSectionSize); try { //Get the section names into the buffer. len = IniAPI.GetPrivateProfileString(argSectionName, null, null, ptr, IniHandler.MaxSectionSize, m_path); retval = ConvertNullSeperatedStringToStringArray(ptr, len); } finally { //Free the buffer Marshal.FreeCoTaskMem(ptr); } return(retval); }
/// <summary> /// Gets all of the values in a section as a list. /// </summary> /// <param name="sectionName"> /// Name of the section to retrieve values from. /// </param> /// <returns> /// A <see cref="List{T}"/> containing <see cref="KeyValuePair{T1, T2}"/> objects /// that describe this section. Use this verison if a section may contain /// multiple items with the same key value. If you know that a section /// cannot contain multiple values with the same key name or you don't /// care about the duplicates, use the more convenient /// <see cref="GetSectionValues"/> function. /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="sectionName"/> is a null reference (Nothing in VB) /// </exception> public List <KeyValuePair <string, string> > GetSectionValuesAsList(string argSectionName) { List <KeyValuePair <string, string> > retval; string[] keyValuePairs; string key, value; int equalSignPos; if (string.IsNullOrEmpty(argSectionName)) { return(null); } //Allocate a buffer for the returned section names. IntPtr ptr = Marshal.AllocCoTaskMem(IniHandler.MaxSectionSize); try { //Get the section key/value pairs into the buffer. int len = IniAPI.GetPrivateProfileSection(argSectionName, ptr, IniHandler.MaxSectionSize, m_path); keyValuePairs = ConvertNullSeperatedStringToStringArray(ptr, len); } finally { //Free the buffer Marshal.FreeCoTaskMem(ptr); } //Parse keyValue pairs and add them to the list. retval = new List <KeyValuePair <string, string> >(keyValuePairs.Length); for (int i = 0; i < keyValuePairs.Length; ++i) { //Parse the "key=value" string into its constituent parts equalSignPos = keyValuePairs[i].IndexOf('='); key = keyValuePairs[i].Substring(0, equalSignPos); value = keyValuePairs[i].Substring(equalSignPos + 1, keyValuePairs[i].Length - equalSignPos - 1); retval.Add(new KeyValuePair <string, string>(key, value)); } return(retval); }
/// <summary> /// Gets the value of a setting in an ini file as a <see cref="T:System.String"/>. /// </summary> /// <param name="argSectionName">The name of the section to read from.</param> /// <param name="argKeyName">The name of the key in section to read.</param> /// <param name="argDefaultValue">The default value to return if the key /// cannot be found.</param> /// <returns>The value of the key, if found. Otherwise, returns /// <paramref name="argDefaultValue"/></returns> /// <remarks> /// The retreived value must be less than 32KB in length. /// </remarks> /// <exception cref="ArgumentNullException"> /// <paramref name="argSectionName"/> or <paramref name="argKeyName"/> are /// a null reference (Nothing in VB) /// </exception> public string GetString(string argSectionName, string argKeyName, string argDefaultValue = null) { if (string.IsNullOrEmpty(argSectionName) || string.IsNullOrEmpty(argKeyName)) { return(null); } StringBuilder retval = new StringBuilder(IniHandler.MaxSectionSize); IniAPI.GetPrivateProfileString(argSectionName, argKeyName, argDefaultValue, retval, IniHandler.MaxSectionSize, m_path); return(retval.ToString()); }
/// <summary> /// Gets the names of all sections in the ini file. /// </summary> /// <returns>An array of section names.</returns> /// <remarks> /// The total length of all section names in the section must be /// less than 32KB in length. /// </remarks> public string[] GetSectionNames() { string[] retval; int len; //Allocate a buffer for the returned section names. IntPtr ptr = Marshal.AllocCoTaskMem(IniHandler.MaxSectionSize); try { //Get the section names into the buffer. len = IniAPI.GetPrivateProfileSectionNames(ptr, IniHandler.MaxSectionSize, m_path); retval = ConvertNullSeperatedStringToStringArray(ptr, len); } finally { //Free the buffer Marshal.FreeCoTaskMem(ptr); } return(retval); }
/// <summary> /// Writes a <see cref="T:System.String"/> value to the ini file. /// </summary> /// <param name="argSectionName">The name of the section to write to .</param> /// <param name="argKeyName">The name of the key to write to.</param> /// <param name="argValue">The string value to write</param> /// <exception cref="T:System.ComponentModel.Win32Exception"> /// The write failed. /// </exception> private bool WriteValueInternal(string argSectionName, string argKeyName, string argValue) { return(IniAPI.WritePrivateProfileString(argSectionName, argKeyName, argValue, m_path)); }