You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
3.0 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. using System;
  2. using System.IO;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5. namespace IPA.Config
  6. {
  7. /// <summary>
  8. /// Create a New INI file to store or load data
  9. /// </summary>
  10. [Obsolete("Jesus, this uses old 16-bit system calls!")]
  11. internal class IniFile
  12. {
  13. [DllImport("KERNEL32.DLL", EntryPoint = "GetPrivateProfileStringW",
  14. SetLastError = true,
  15. CharSet = CharSet.Unicode, ExactSpelling = true,
  16. CallingConvention = CallingConvention.StdCall)]
  17. private static extern int GetPrivateProfileString(
  18. string lpSection,
  19. string lpKey,
  20. string lpDefault,
  21. StringBuilder lpReturnString,
  22. int nSize,
  23. string lpFileName);
  24. [DllImport("KERNEL32.DLL", EntryPoint = "WritePrivateProfileStringW",
  25. SetLastError = true,
  26. CharSet = CharSet.Unicode, ExactSpelling = true,
  27. CallingConvention = CallingConvention.StdCall)]
  28. private static extern int WritePrivateProfileString(
  29. string lpSection,
  30. string lpKey,
  31. string lpValue,
  32. string lpFileName);
  33. /*private string _path = "";
  34. public string Path
  35. {
  36. get
  37. {
  38. return _path;
  39. }
  40. set
  41. {
  42. if (!File.Exists(value))
  43. File.WriteAllText(value, "", Encoding.Unicode);
  44. _path = value;
  45. }
  46. }*/
  47. private FileInfo _iniFileInfo;
  48. public FileInfo IniFileInfo {
  49. get => _iniFileInfo;
  50. set {
  51. _iniFileInfo = value;
  52. if (_iniFileInfo.Exists) return;
  53. _iniFileInfo.Directory?.Create();
  54. _iniFileInfo.Create();
  55. }
  56. }
  57. /// <summary>
  58. /// INIFile Constructor.
  59. /// </summary>
  60. /// <PARAM name="iniPath"></PARAM>
  61. public IniFile(string iniPath)
  62. {
  63. IniFileInfo = new FileInfo(iniPath);
  64. //this.Path = INIPath;
  65. }
  66. /// <summary>
  67. /// Write Data to the INI File
  68. /// </summary>
  69. /// <PARAM name="section"></PARAM>
  70. /// Section name
  71. /// <PARAM name="key"></PARAM>
  72. /// Key Name
  73. /// <PARAM name="value"></PARAM>
  74. /// Value Name
  75. public void IniWriteValue(string section, string key, string value)
  76. {
  77. WritePrivateProfileString(section, key, value, IniFileInfo.FullName);
  78. }
  79. /// <summary>
  80. /// Read Data Value From the Ini File
  81. /// </summary>
  82. /// <PARAM name="section"></PARAM>
  83. /// <PARAM name="key"></PARAM>
  84. /// <returns></returns>
  85. public string IniReadValue(string section, string key)
  86. {
  87. const int maxChars = 1023;
  88. StringBuilder result = new StringBuilder(maxChars);
  89. GetPrivateProfileString(section, key, "", result, maxChars, IniFileInfo.FullName);
  90. return result.ToString();
  91. }
  92. }
  93. }