Ini.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Windows.Forms;
  6. using System.Runtime.InteropServices;
  7. using System.Collections;
  8. using System.Globalization;
  9. namespace BaseLibRWFile
  10. {
  11. public class Ini
  12. {
  13. public const int MaxSectionSize = 32767; // 32 KB
  14. private string m_path;
  15. private Dictionary<string, object> _locks = new Dictionary<string, object>();
  16. #region P/Invoke declares
  17. [System.Security.SuppressUnmanagedCodeSecurity]
  18. private static class NativeMethods
  19. {
  20. [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
  21. public static extern int GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer,
  22. uint nSize,
  23. string lpFileName);
  24. [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
  25. public static extern uint GetPrivateProfileString(string lpAppName,
  26. string lpKeyName,
  27. string lpDefault,
  28. StringBuilder lpReturnedString,
  29. int nSize,
  30. string lpFileName);
  31. [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
  32. public static extern uint GetPrivateProfileString(string lpAppName,
  33. string lpKeyName,
  34. string lpDefault,
  35. [In, Out] char[] lpReturnedString,
  36. int nSize,
  37. string lpFileName);
  38. [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
  39. public static extern int GetPrivateProfileString(string lpAppName,
  40. string lpKeyName,
  41. string lpDefault,
  42. IntPtr lpReturnedString,
  43. uint nSize,
  44. string lpFileName);
  45. [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
  46. public static extern int GetPrivateProfileInt(string lpAppName,
  47. string lpKeyName,
  48. int lpDefault,
  49. string lpFileName);
  50. [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
  51. public static extern int GetPrivateProfileSection(string lpAppName,
  52. IntPtr lpReturnedString,
  53. uint nSize,
  54. string lpFileName);
  55. [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  56. public static extern bool WritePrivateProfileString(string lpAppName,
  57. string lpKeyName,
  58. string lpString,
  59. string lpFileName);
  60. [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  61. public static extern bool WritePrivateProfileSection(string lpAppName,
  62. string lpstring,
  63. string lpFileName);
  64. }
  65. #endregion
  66. /// <summary>
  67. /// 实例化Ini
  68. /// </summary>
  69. /// <param name="path">ini文件完整路径</param>
  70. /// <returns></returns>
  71. public Ini(string path)
  72. {
  73. m_path = System.IO.Path.GetFullPath(path);
  74. if (!_locks.ContainsKey(m_path))
  75. {
  76. _locks.Add(m_path, new object());
  77. }
  78. }
  79. /// <summary>
  80. /// 读取ini文件指定数据
  81. /// </summary>
  82. /// <param name="label">数据标签</param>
  83. /// <param name="name">数据名称</param>
  84. /// <param name="value">默认值</param>
  85. /// <returns></returns>
  86. public string ReadIni(string label, string name, String value) //读配置文件
  87. {
  88. try
  89. {
  90. string ret;
  91. ret = GetString(label, name, value);
  92. if (ret == value)
  93. {
  94. WriteValue(label, name, value);
  95. }
  96. return ret;
  97. }
  98. catch (Exception ex)
  99. {
  100. MessageBox.Show(ex.ToString(), "ReadIni Error");
  101. return null;
  102. }
  103. }
  104. /// <summary>
  105. /// 写入数据到ini文件
  106. /// </summary>
  107. /// <param name="label">数据标签</param>
  108. /// <param name="name">数据名称</param>
  109. /// <param name="value">要写入的值</param>
  110. /// <returns></returns>
  111. public void WriteIni(string label, string name, String value) //写配置文件
  112. {
  113. try
  114. {
  115. WriteValue(label, name, value);
  116. }
  117. catch (Exception ex)
  118. {
  119. MessageBox.Show(ex.ToString(), "WriteIni Error");
  120. }
  121. }
  122. public string Path
  123. {
  124. get
  125. {
  126. return m_path;
  127. }
  128. }
  129. /// <summary>
  130. /// 创建文件夹
  131. /// </summary>
  132. /// <param name="DirectoryFileName">文件夹完整路径</param>
  133. /// <returns></returns>
  134. public void DirectoryCreate(string DirectoryFileName)
  135. {
  136. if (!Directory.Exists(DirectoryFileName))
  137. {
  138. Directory.CreateDirectory(DirectoryFileName);
  139. }
  140. }
  141. /// <summary>
  142. /// 创建ini文件
  143. /// </summary>
  144. /// <param name="FileName">ini文件完整路径</param>
  145. /// <returns></returns>
  146. public void CreateFile(string FileName)
  147. {
  148. if (!(File.Exists(FileName)))
  149. {
  150. File.Create(FileName).Close();
  151. }
  152. }
  153. public void CopyFile(string strSourcePath, string strTargetPath, bool IsCoverage)
  154. {
  155. try
  156. {
  157. if (!File.Exists(strTargetPath))
  158. {
  159. CreateFile(strTargetPath);
  160. }
  161. File.Copy(strSourcePath, strTargetPath, IsCoverage);
  162. }
  163. catch
  164. { }
  165. }
  166. #region Get Value Methods
  167. public string GetString(string sectionName,
  168. string keyName,
  169. string defaultValue)
  170. {
  171. if (sectionName == null)
  172. throw new ArgumentNullException(nameof(sectionName));
  173. if (keyName == null)
  174. throw new ArgumentNullException(nameof(keyName));
  175. lock (_locks[m_path])
  176. {
  177. StringBuilder retval = new StringBuilder(Ini.MaxSectionSize);
  178. NativeMethods.GetPrivateProfileString(sectionName,
  179. keyName,
  180. defaultValue,
  181. retval,
  182. Ini.MaxSectionSize,
  183. m_path);
  184. return retval.ToString();
  185. }
  186. }
  187. /// <summary>
  188. /// Gets the value of a setting in an ini file as a <see cref="T:System.Int16"/>.
  189. /// </summary>
  190. /// <param name="sectionName">The name of the section to read from.</param>
  191. /// <param name="keyName">The name of the key in section to read.</param>
  192. /// <param name="defaultValue">The default value to return if the key
  193. /// cannot be found.</param>
  194. /// <returns>The value of the key, if found. Otherwise, returns
  195. /// <paramref name="defaultValue"/>.</returns>
  196. /// <exception cref="ArgumentNullException">
  197. /// <paramref name="sectionName"/> or <paramref name="keyName"/> are
  198. /// a null reference (Nothing in VB)
  199. /// </exception>
  200. public int GetInt16(string sectionName,
  201. string keyName,
  202. short defaultValue)
  203. {
  204. int retval = GetInt32(sectionName, keyName, defaultValue);
  205. return Convert.ToInt16(retval);
  206. }
  207. /// <summary>
  208. /// Gets the value of a setting in an ini file as a <see cref="T:System.Int32"/>.
  209. /// </summary>
  210. /// <param name="sectionName">The name of the section to read from.</param>
  211. /// <param name="keyName">The name of the key in section to read.</param>
  212. /// <param name="defaultValue">The default value to return if the key
  213. /// cannot be found.</param>
  214. /// <returns>The value of the key, if found. Otherwise, returns
  215. /// <paramref name="defaultValue"/></returns>
  216. /// <exception cref="ArgumentNullException">
  217. /// <paramref name="sectionName"/> or <paramref name="keyName"/> are
  218. /// a null reference (Nothing in VB)
  219. /// </exception>
  220. public int GetInt32(string sectionName,
  221. string keyName,
  222. int defaultValue)
  223. {
  224. if (sectionName == null)
  225. throw new ArgumentNullException(nameof(sectionName));
  226. if (keyName == null)
  227. throw new ArgumentNullException(nameof(keyName));
  228. lock (_locks[m_path])
  229. {
  230. return NativeMethods.GetPrivateProfileInt(sectionName, keyName, defaultValue, m_path);
  231. }
  232. }
  233. /// <summary>
  234. /// Gets the value of a setting in an ini file as a <see cref="T:System.Double"/>.
  235. /// </summary>
  236. /// <param name="sectionName">The name of the section to read from.</param>
  237. /// <param name="keyName">The name of the key in section to read.</param>
  238. /// <param name="defaultValue">The default value to return if the key
  239. /// cannot be found.</param>
  240. /// <returns>The value of the key, if found. Otherwise, returns
  241. /// <paramref name="defaultValue"/></returns>
  242. /// <exception cref="ArgumentNullException">
  243. /// <paramref name="sectionName"/> or <paramref name="keyName"/> are
  244. /// a null reference (Nothing in VB)
  245. /// </exception>
  246. public double GetDouble(string sectionName,
  247. string keyName,
  248. double defaultValue)
  249. {
  250. string retval = GetString(sectionName, keyName, "");
  251. if (retval == null || retval.Length == 0)
  252. {
  253. return defaultValue;
  254. }
  255. return Convert.ToDouble(retval, CultureInfo.InvariantCulture);
  256. }
  257. #endregion
  258. #region GetSectionValues Methods
  259. /// <summary>
  260. /// Gets all of the values in a section as a list.
  261. /// </summary>
  262. /// <param name="sectionName">
  263. /// Name of the section to retrieve values from.
  264. /// </param>
  265. /// <returns>
  266. /// A <see cref="List{T}"/> containing <see cref="KeyValuePair{T1, T2}"/> objects
  267. /// that describe this section. Use this verison if a section may contain
  268. /// multiple items with the same key value. If you know that a section
  269. /// cannot contain multiple values with the same key name or you don't
  270. /// care about the duplicates, use the more convenient
  271. /// <see cref="GetSectionValues"/> function.
  272. /// </returns>
  273. /// <exception cref="ArgumentNullException">
  274. /// <paramref name="sectionName"/> is a null reference (Nothing in VB)
  275. /// </exception>
  276. public List<KeyValuePair<string, string>> GetSectionValuesAsList(string sectionName)
  277. {
  278. List<KeyValuePair<string, string>> retval;
  279. string[] keyValuePairs;
  280. string key, value;
  281. int equalSignPos;
  282. if (sectionName == null)
  283. throw new ArgumentNullException(nameof(sectionName));
  284. //Allocate a buffer for the returned section names.
  285. IntPtr ptr = Marshal.AllocCoTaskMem(Ini.MaxSectionSize);
  286. try
  287. {
  288. //Get the section key/value pairs into the buffer.
  289. lock (_locks[m_path])
  290. {
  291. int len = NativeMethods.GetPrivateProfileSection(sectionName,
  292. ptr,
  293. Ini.MaxSectionSize,
  294. m_path);
  295. keyValuePairs = ConvertNullSeperatedStringToStringArray(ptr, len);
  296. }
  297. }
  298. finally
  299. {
  300. //Free the buffer
  301. Marshal.FreeCoTaskMem(ptr);
  302. }
  303. //Parse keyValue pairs and add them to the list.
  304. retval = new List<KeyValuePair<string, string>>(keyValuePairs.Length);
  305. for (int i = 0; i < keyValuePairs.Length; ++i)
  306. {
  307. //Parse the "key=value" string into its constituent parts
  308. equalSignPos = keyValuePairs[i].IndexOf('=');
  309. key = keyValuePairs[i].Substring(0, equalSignPos);
  310. value = keyValuePairs[i].Substring(equalSignPos + 1,
  311. keyValuePairs[i].Length - equalSignPos - 1);
  312. retval.Add(new KeyValuePair<string, string>(key, value));
  313. }
  314. return retval;
  315. }
  316. /// <summary>
  317. /// Gets all of the values in a section as a dictionary.
  318. /// </summary>
  319. /// <param name="sectionName">
  320. /// Name of the section to retrieve values from.
  321. /// </param>
  322. /// <returns>
  323. /// A <see cref="Dictionary{T, T}"/> containing the key/value
  324. /// pairs found in this section.
  325. /// </returns>
  326. /// <remarks>
  327. /// If a section contains more than one key with the same name,
  328. /// this function only returns the first instance. If you need to
  329. /// get all key/value pairs within a section even when keys have the
  330. /// same name, use <see cref="GetSectionValuesAsList"/>.
  331. /// </remarks>
  332. /// <exception cref="ArgumentNullException">
  333. /// <paramref name="sectionName"/> is a null reference (Nothing in VB)
  334. /// </exception>
  335. public Dictionary<string, string> GetSectionValues(string sectionName)
  336. {
  337. List<KeyValuePair<string, string>> keyValuePairs;
  338. Dictionary<string, string> retval;
  339. keyValuePairs = GetSectionValuesAsList(sectionName);
  340. //Convert list into a dictionary.
  341. retval = new Dictionary<string, string>(keyValuePairs.Count);
  342. foreach (KeyValuePair<string, string> keyValuePair in keyValuePairs)
  343. {
  344. //Skip any key we have already seen.
  345. if (!retval.ContainsKey(keyValuePair.Key))
  346. {
  347. retval.Add(keyValuePair.Key, keyValuePair.Value);
  348. }
  349. }
  350. return retval;
  351. }
  352. #endregion
  353. #region Get Key/Section Names
  354. /// <summary>
  355. /// Gets the names of all keys under a specific section in the ini file.
  356. /// </summary>
  357. /// <param name="sectionName">
  358. /// The name of the section to read key names from.
  359. /// </param>
  360. /// <returns>An array of key names.</returns>
  361. /// <remarks>
  362. /// The total length of all key names in the section must be
  363. /// less than 32KB in length.
  364. /// </remarks>
  365. /// <exception cref="ArgumentNullException">
  366. /// <paramref name="sectionName"/> is a null reference (Nothing in VB)
  367. /// </exception>
  368. public string[] GetKeyNames(string sectionName)
  369. {
  370. int len;
  371. string[] retval;
  372. if (sectionName == null)
  373. throw new ArgumentNullException(nameof(sectionName));
  374. //Allocate a buffer for the returned section names.
  375. IntPtr ptr = Marshal.AllocCoTaskMem(Ini.MaxSectionSize);
  376. try
  377. {
  378. //Get the section names into the buffer.
  379. lock (_locks[m_path])
  380. {
  381. len = NativeMethods.GetPrivateProfileString(sectionName,
  382. null,
  383. null,
  384. ptr,
  385. Ini.MaxSectionSize,
  386. m_path);
  387. retval = ConvertNullSeperatedStringToStringArray(ptr, len);
  388. }
  389. }
  390. finally
  391. {
  392. //Free the buffer
  393. Marshal.FreeCoTaskMem(ptr);
  394. }
  395. return retval;
  396. }
  397. /// <summary>
  398. /// Gets the names of all sections in the ini file.
  399. /// </summary>
  400. /// <returns>An array of section names.</returns>
  401. /// <remarks>
  402. /// The total length of all section names in the section must be
  403. /// less than 32KB in length.
  404. /// </remarks>
  405. public string[] GetSectionNames()
  406. {
  407. string[] retval;
  408. int len;
  409. //Allocate a buffer for the returned section names.
  410. IntPtr ptr = Marshal.AllocCoTaskMem(Ini.MaxSectionSize);
  411. try
  412. {
  413. //Get the section names into the buffer.
  414. lock (_locks[m_path])
  415. {
  416. len = NativeMethods.GetPrivateProfileSectionNames(ptr,
  417. Ini.MaxSectionSize, m_path);
  418. retval = ConvertNullSeperatedStringToStringArray(ptr, len);
  419. }
  420. }
  421. finally
  422. {
  423. //Free the buffer
  424. Marshal.FreeCoTaskMem(ptr);
  425. }
  426. return retval;
  427. }
  428. /// <summary>
  429. /// Converts the null seperated pointer to a string into a string array.
  430. /// </summary>
  431. /// <param name="ptr">A pointer to string data.</param>
  432. /// <param name="valLength">
  433. /// Length of the data pointed to by <paramref name="ptr"/>.
  434. /// </param>
  435. /// <returns>
  436. /// An array of strings; one for each null found in the array of characters pointed
  437. /// at by <paramref name="ptr"/>.
  438. /// </returns>
  439. private static string[] ConvertNullSeperatedStringToStringArray(IntPtr ptr, int valLength)
  440. {
  441. string[] retval;
  442. if (valLength == 0)
  443. {
  444. //Return an empty array.
  445. retval = Array.Empty<string>();
  446. }
  447. else
  448. {
  449. //Convert the buffer into a string. Decrease the length
  450. //by 1 so that we remove the second null off the end.
  451. string buff = Marshal.PtrToStringAuto(ptr, valLength - 1);
  452. //Parse the buffer into an array of strings by searching for nulls.
  453. retval = buff.Split('\0');
  454. }
  455. return retval;
  456. }
  457. #endregion
  458. #region Write Methods
  459. /// <summary>
  460. /// Writes a <see cref="T:System.String"/> value to the ini file.
  461. /// </summary>
  462. /// <param name="sectionName">The name of the section to write to .</param>
  463. /// <param name="keyName">The name of the key to write to.</param>
  464. /// <param name="value">The string value to write</param>
  465. /// <exception cref="T:System.ComponentModel.Win32Exception">
  466. /// The write failed.
  467. /// </exception>
  468. private void WriteValueInternal(string sectionName, string keyName, string value)
  469. {
  470. lock (_locks[m_path])
  471. {
  472. if (!NativeMethods.WritePrivateProfileString(sectionName, keyName, value, m_path))
  473. {
  474. throw new System.ComponentModel.Win32Exception();
  475. }
  476. }
  477. }
  478. public void WriteValue(string sectionName)
  479. {
  480. WriteValueInternal(sectionName, "a", "a");
  481. WriteValueInternal(sectionName, "a", null);
  482. //NativeMethods.WritePrivateProfileSection(sectionName, null, m_path);
  483. }
  484. /// <summary>
  485. /// Writes a <see cref="T:System.String"/> value to the ini file.
  486. /// </summary>
  487. /// <param name="sectionName">The name of the section to write to .</param>
  488. /// <param name="keyName">The name of the key to write to.</param>
  489. /// <param name="value">The string value to write</param>
  490. /// <exception cref="T:System.ComponentModel.Win32Exception">
  491. /// The write failed.
  492. /// </exception>
  493. /// <exception cref="ArgumentNullException">
  494. /// <paramref name="sectionName"/> or <paramref name="keyName"/> or
  495. /// <paramref name="value"/> are a null reference (Nothing in VB)
  496. /// </exception>
  497. public void WriteValue(string sectionName, string keyName, string value)
  498. {
  499. if (sectionName == null)
  500. throw new ArgumentNullException(nameof(sectionName));
  501. if (keyName == null)
  502. throw new ArgumentNullException(nameof(keyName));
  503. if (value == null)
  504. throw new ArgumentNullException(nameof(value));
  505. WriteValueInternal(sectionName, keyName, value);
  506. }
  507. /// <summary>
  508. /// Writes an <see cref="T:System.Int16"/> value to the ini file.
  509. /// </summary>
  510. /// <param name="sectionName">The name of the section to write to .</param>
  511. /// <param name="keyName">The name of the key to write to.</param>
  512. /// <param name="value">The value to write</param>
  513. /// <exception cref="T:System.ComponentModel.Win32Exception">
  514. /// The write failed.
  515. /// </exception>
  516. public void WriteValue(string sectionName, string keyName, short value)
  517. {
  518. WriteValue(sectionName, keyName, (int)value);
  519. }
  520. /// <summary>
  521. /// Writes an <see cref="T:System.Int32"/> value to the ini file.
  522. /// </summary>
  523. /// <param name="sectionName">The name of the section to write to .</param>
  524. /// <param name="keyName">The name of the key to write to.</param>
  525. /// <param name="value">The value to write</param>
  526. /// <exception cref="T:System.ComponentModel.Win32Exception">
  527. /// The write failed.
  528. /// </exception>
  529. /// <exception cref="ArgumentNullException">
  530. /// <paramref name="sectionName"/> or <paramref name="keyName"/> are
  531. /// a null reference (Nothing in VB)
  532. /// </exception>
  533. public void WriteValue(string sectionName, string keyName, int value)
  534. {
  535. WriteValue(sectionName, keyName, value.ToString(CultureInfo.InvariantCulture));
  536. }
  537. /// <summary>
  538. /// Writes an <see cref="T:System.Single"/> value to the ini file.
  539. /// </summary>
  540. /// <param name="sectionName">The name of the section to write to .</param>
  541. /// <param name="keyName">The name of the key to write to.</param>
  542. /// <param name="value">The value to write</param>
  543. /// <exception cref="T:System.ComponentModel.Win32Exception">
  544. /// The write failed.
  545. /// </exception>
  546. /// <exception cref="ArgumentNullException">
  547. /// <paramref name="sectionName"/> or <paramref name="keyName"/> are
  548. /// a null reference (Nothing in VB)
  549. /// </exception>
  550. public void WriteValue(string sectionName, string keyName, float value)
  551. {
  552. WriteValue(sectionName, keyName, value.ToString(CultureInfo.InvariantCulture));
  553. }
  554. /// <summary>
  555. /// Writes an <see cref="T:System.Double"/> value to the ini file.
  556. /// </summary>
  557. /// <param name="sectionName">The name of the section to write to .</param>
  558. /// <param name="keyName">The name of the key to write to.</param>
  559. /// <param name="value">The value to write</param>
  560. /// <exception cref="T:System.ComponentModel.Win32Exception">
  561. /// The write failed.
  562. /// </exception>
  563. /// <exception cref="ArgumentNullException">
  564. /// <paramref name="sectionName"/> or <paramref name="keyName"/> are
  565. /// a null reference (Nothing in VB)
  566. /// </exception>
  567. public void WriteValue(string sectionName, string keyName, double value)
  568. {
  569. WriteValue(sectionName, keyName, value.ToString(CultureInfo.InvariantCulture));
  570. }
  571. #endregion
  572. #region Delete Methods
  573. /// <summary>
  574. /// Deletes the specified key from the specified section.
  575. /// </summary>
  576. /// <param name="sectionName">
  577. /// Name of the section to remove the key from.
  578. /// </param>
  579. /// <param name="keyName">
  580. /// Name of the key to remove.
  581. /// </param>
  582. /// <exception cref="ArgumentNullException">
  583. /// <paramref name="sectionName"/> or <paramref name="keyName"/> are
  584. /// a null reference (Nothing in VB)
  585. /// </exception>
  586. public void DeleteKey(string sectionName, string keyName)
  587. {
  588. if (sectionName == null)
  589. throw new ArgumentNullException(nameof(sectionName));
  590. if (keyName == null)
  591. throw new ArgumentNullException(nameof(keyName));
  592. //ArrayList list = ArrayList.Adapter(GetSectionNames());
  593. //if (!list.Contains(sectionName))
  594. //{
  595. // return;
  596. //}
  597. //list = ArrayList.Adapter(GetKeyNames(sectionName));
  598. //if (!list.Contains(keyName))
  599. //{
  600. // return;
  601. //}
  602. WriteValueInternal(sectionName, keyName, null);
  603. }
  604. /// <summary>
  605. /// Deletes a section from the ini file.
  606. /// </summary>
  607. /// <param name="sectionName">
  608. /// Name of the section to delete.
  609. /// </param>
  610. /// <exception cref="ArgumentNullException">
  611. /// <paramref name="sectionName"/> is a null reference (Nothing in VB)
  612. /// </exception>
  613. public void DeleteSection(string sectionName)
  614. {
  615. if (sectionName == null)
  616. throw new ArgumentNullException(nameof(sectionName));
  617. ArrayList list = ArrayList.Adapter(GetSectionNames());
  618. if (list.Contains(sectionName))
  619. {
  620. WriteValueInternal(sectionName, null, null);
  621. }
  622. }
  623. #endregion
  624. }
  625. }