_Ini.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.InteropServices;
  6. using System.IO;
  7. namespace FPER
  8. {
  9. public static class _Ini
  10. {
  11. public static class IniControl
  12. {
  13. // 라이팅
  14. [DllImport("kernel32")]
  15. public static extern bool WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName);
  16. // int 읽기
  17. [DllImport("kernel32")]
  18. public static extern uint GetPrivateProfileInt(string lpAppName, string lpKeyName, int nDefault, string lpFileName);
  19. // string 읽기
  20. [DllImport("kernel32")]
  21. public static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);
  22. }
  23. // .ini 파일 구성
  24. // [Section1]
  25. // Key1=Value1
  26. // Key2=Value2
  27. // [Section2]
  28. // Key1=Value1
  29. // Key2=Value2
  30. // ex) test.ini
  31. // [삼성]
  32. // 이름=라이온스
  33. // [롯데]
  34. // 이름=
  35. // 읽기(예시)
  36. // OK
  37. // _Ini.Read_Ini("삼성", "이름", 512, "C:\\Documents and Settings\\gadget81\\My Documents\\Visual Studio 2010\\Projects\\INI\\test.ini")
  38. // 리턴문을 확인하면, "라이온스" 확인 할수 있음
  39. // NO
  40. // _Ini.Read_Ini("NAME", "이름", 512, "C:\\Documents and Settings\\gadget81\\My Documents\\Visual Studio 2010\\Projects\\INI\\test.ini")
  41. // 섹션이름이나 키이름을 잘못사용 할 경우 "" 리턴하게 된다. 이런 경우는 코딩만 잘 한다면 없을 것임.
  42. // 읽기
  43. public static string Read_Ini(string Section, string Key, int Size, string Path)
  44. {
  45. try
  46. {
  47. // string 읽기
  48. StringBuilder gStringBuilder = new StringBuilder(Size);
  49. IniControl.GetPrivateProfileString(Section, Key, "", gStringBuilder, Size, Path);
  50. return gStringBuilder.ToString();
  51. }
  52. catch
  53. {
  54. return null;
  55. }
  56. }
  57. // 라이트(예시)
  58. // OK
  59. // _Ini.Write_Ini("롯데", "이름", "자이언트", "C:\\Documents and Settings\\gadget81\\My Documents\\Visual Studio 2010\\Projects\\INI\\test.ini")
  60. // 해당 함수를 호출하게 되면 "롯데"섹션에 "이름"키에 "자이언트"를 라이팅하게 된다
  61. // 라이트
  62. public static void Write_Ini(string Section, string Key, string Value, string Path)
  63. {
  64. try
  65. {
  66. IniControl.WritePrivateProfileString(Section, Key, Value, Path);
  67. }
  68. catch
  69. {
  70. }
  71. }
  72. // 파일 생성
  73. // Path는 "C:\\Documents and Settings\\gadget81\\My Documents\\Visual Studio 2010\\Projects\\INI\\test.ini"
  74. public static bool Create_Ini(string txt, string path)
  75. {
  76. try
  77. {
  78. // 파일이 존재하면 삭제
  79. if (File.Exists(path))
  80. {
  81. File.Delete(path);
  82. }
  83. // 파일 생성
  84. File.WriteAllText(path, txt);
  85. // 성공
  86. return true;
  87. }
  88. catch //(Exception e)
  89. {
  90. // LOG
  91. ////_Event.DebugView_SendMessage_Write(e.ToString());(e.ToString());
  92. // 실패
  93. return false;
  94. }
  95. }
  96. // 파일 삭제
  97. // Path는 "C:\\Documents and Settings\\gadget81\\My Documents\\Visual Studio 2010\\Projects\\INI\\test.ini"
  98. public static void Delete_Ini(string path)
  99. {
  100. try
  101. {
  102. // 파일이 존재하면 삭제
  103. if (File.Exists(path))
  104. {
  105. File.Delete(path);
  106. }
  107. }
  108. catch
  109. {
  110. }
  111. }
  112. }
  113. }