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.

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