/XAct.IO/XAct.IO.FS/IniFileManager.cs

https://bitbucket.org/xact/cs.ff.xact.lib · C# · 244 lines · 147 code · 21 blank · 76 comment · 13 complexity · 99b149efe7724c60d4fdc938de499c0c MD5 · raw file

  1. #undef INCLUDE_EXAMPLES
  2. namespace XAct.IO
  3. {
  4. using System;
  5. using System.Collections.Specialized;
  6. using System.Runtime.InteropServices;
  7. using System.Text;
  8. ///<summary>
  9. /// Class to handle INI file I/O
  10. ///</summary>
  11. ///<summary>
  12. /// Constructor
  13. ///</summary>
  14. ///<remarks>
  15. /// <para>
  16. /// I've built this class too many times in too many languages...this time, the code was found for free at: http://archive.devx.com/dotnet/discussions/040902/cominterop.asp, thank you!...Although he does end with:
  17. /// this code is meant for example use only. You should add error-trapping and checking (see the DllImportAttribute.SetLastError field in the documentation, and the Marshal.GetLastWin32Error method in the documentation for more information.)
  18. /// </para>
  19. /// <para>
  20. /// ToDo:
  21. /// Extend to use XML or INI, according to article:
  22. /// http://www.devx.com/dotnet/Article/7008/0/page/1
  23. /// </para>
  24. ///</remarks>
  25. public class IniFileManager
  26. {
  27. /// <summary>
  28. /// Wrapper for API Call
  29. /// </summary>
  30. [DllImport("KERNEL32.DLL",
  31. EntryPoint = "GetPrivateProfileString")]
  32. protected internal static extern int
  33. GetPrivateProfileString(string lpAppName,
  34. string lpKeyName, string lpDefault,
  35. StringBuilder lpReturnedString, int nSize,
  36. string lpFileName);
  37. /// <summary>
  38. /// Wrapper for API Call
  39. /// </summary>
  40. [DllImport("KERNEL32.DLL")]
  41. protected internal static extern int
  42. GetPrivateProfileInt(string lpAppName,
  43. string lpKeyName, int iDefault,
  44. string lpFileName);
  45. /// <summary>
  46. /// Wrapper for API Call
  47. /// </summary>
  48. [DllImport("KERNEL32.DLL",
  49. EntryPoint = "WritePrivateProfileString")]
  50. protected internal static extern bool
  51. WritePrivateProfileString(string lpAppName,
  52. string lpKeyName, string lpString,
  53. string lpFileName);
  54. /// <summary>
  55. /// Wrapper for API Call
  56. /// </summary>
  57. [DllImport("KERNEL32.DLL",
  58. EntryPoint = "GetPrivateProfileSection")]
  59. protected internal static extern int
  60. GetPrivateProfileSection(string lpAppName,
  61. byte[] lpReturnedString, int nSize,
  62. string lpFileName);
  63. /// <summary>
  64. /// Wrapper for API Call
  65. /// </summary>
  66. [DllImport("KERNEL32.DLL",
  67. EntryPoint = "WritePrivateProfileSection")]
  68. protected internal static extern bool
  69. WritePrivateProfileSection(string lpAppName,
  70. byte[] data, string lpFileName);
  71. /// <summary>
  72. /// Wrapper for API Call
  73. /// </summary>
  74. [DllImport("KERNEL32.DLL",
  75. EntryPoint = "GetPrivateProfileSectionNames")]
  76. protected internal static extern int
  77. GetPrivateProfileSectionNames(
  78. byte[] lpReturnedString,
  79. int nSize, string lpFileName);
  80. /// <summary>
  81. /// Returns Value from INI file.
  82. /// </summary>
  83. /// <param name = "fileName"></param>
  84. /// <param name = "section"></param>
  85. /// <param name = "key"></param>
  86. /// <returns></returns>
  87. public static String GetIniValue(String fileName,
  88. String section, String key)
  89. {
  90. StringBuilder buffer = new StringBuilder(256);
  91. string sDefault = "";
  92. if (GetPrivateProfileString(section, key, sDefault,
  93. buffer, buffer.Capacity, fileName) != 0)
  94. {
  95. return buffer.ToString();
  96. }
  97. return null;
  98. }
  99. /// <summary>
  100. /// Writes string to an INI file.
  101. /// </summary>
  102. /// <param name = "fileName"></param>
  103. /// <param name = "section"></param>
  104. /// <param name = "key"></param>
  105. /// <param name = "sValue"></param>
  106. /// <returns></returns>
  107. public static bool WriteIniValue(String fileName,
  108. String section, String key, String sValue)
  109. {
  110. return WritePrivateProfileString(section, key,
  111. sValue, fileName);
  112. }
  113. /// <summary>
  114. /// Get Int value from an INI File.
  115. /// </summary>
  116. /// <param name = "fileName"></param>
  117. /// <param name = "section"></param>
  118. /// <param name = "key"></param>
  119. /// <returns></returns>
  120. public static int GetIniInt(String fileName,
  121. String section, String key)
  122. {
  123. int iDefault = -1;
  124. var result = GetPrivateProfileInt(section, key, iDefault, fileName);
  125. return result;
  126. }
  127. /// <summary>
  128. /// Gets List of Values from an INI section.
  129. /// </summary>
  130. /// <param name = "fileName"></param>
  131. /// <param name = "section"></param>
  132. /// <returns></returns>
  133. public static StringCollection GetIniSection(String fileName, String section)
  134. {
  135. if (string.IsNullOrEmpty(fileName))
  136. {
  137. throw new ArgumentNullException("fileName");
  138. }
  139. StringCollection items = new StringCollection();
  140. byte[] buffer = new byte[32768];
  141. int bufLen;
  142. bufLen = GetPrivateProfileSection(section, buffer,
  143. buffer.GetUpperBound(0), fileName);
  144. if (bufLen > 0)
  145. {
  146. StringBuilder sb = new StringBuilder();
  147. for (int i = 0; i < bufLen; i++)
  148. {
  149. if (buffer[i] != 0)
  150. {
  151. sb.Append((char) buffer[i]);
  152. }
  153. else
  154. {
  155. if (sb.Length > 0)
  156. {
  157. items.Add(sb.ToString());
  158. sb = new StringBuilder();
  159. }
  160. }
  161. }
  162. }
  163. return items;
  164. }
  165. /// <summary>
  166. /// Writes a List to INI Section
  167. /// </summary>
  168. /// <param name = "fileName"></param>
  169. /// <param name = "section"></param>
  170. /// <param name = "items"></param>
  171. /// <returns></returns>
  172. public static bool WriteIniSection(string fileName, string
  173. section, StringCollection items)
  174. {
  175. byte[] b = new byte[32768];
  176. int j = 0;
  177. foreach (string s in items)
  178. {
  179. //ASCII is not available on PCL, so switching to UTF8:
  180. Encoding.UTF8.GetBytes(s, 0, s.Length, b, j);
  181. j += s.Length;
  182. b[j] = 0;
  183. j += 1;
  184. }
  185. b[j] = 0;
  186. return WritePrivateProfileSection(section, b, fileName);
  187. }
  188. /// <summary>
  189. /// Get name of all sections in INI File.
  190. /// </summary>
  191. /// <param name = "fileName"></param>
  192. /// <returns></returns>
  193. public static StringCollection GetIniSectionNames(
  194. String fileName)
  195. {
  196. StringCollection sections = new StringCollection();
  197. byte[] buffer = new byte[32768];
  198. int bufLen;
  199. bufLen = GetPrivateProfileSectionNames(buffer,
  200. buffer.GetUpperBound(0), fileName);
  201. if (bufLen > 0)
  202. {
  203. StringBuilder sb = new StringBuilder();
  204. for (int i = 0; i < bufLen; i++)
  205. {
  206. if (buffer[i] != 0)
  207. {
  208. sb.Append((char) buffer[i]);
  209. }
  210. else
  211. {
  212. if (sb.Length > 0)
  213. {
  214. sections.Add(sb.ToString());
  215. sb = new StringBuilder();
  216. }
  217. }
  218. }
  219. }
  220. return sections;
  221. }
  222. }
  223. }