using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Runtime.InteropServices; namespace IControls_FireManager { public static class _Ini { public static class IniControl { // 라이팅 [DllImport("kernel32")] public static extern bool WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName); // int 읽기 [DllImport("kernel32")] public static extern uint GetPrivateProfileInt(string lpAppName, string lpKeyName, int nDefault, string lpFileName); // string 읽기 [DllImport("kernel32")] public static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName); } // .ini 파일 구성 // [Section1] // Key1=Value1 // Key2=Value2 // [Section2] // Key1=Value1 // Key2=Value2 // ex) test.ini // [삼성] // 이름=라이온스 // [롯데] // 이름= // 읽기(예시) // OK // _Ini.Read_Ini("삼성", "이름", 512, "C:\\Documents and Settings\\gadget81\\My Documents\\Visual Studio 2010\\Projects\\INI\\test.ini") // 리턴문을 확인하면, "라이온스" 확인 할수 있음 // NO // _Ini.Read_Ini("NAME", "이름", 512, "C:\\Documents and Settings\\gadget81\\My Documents\\Visual Studio 2010\\Projects\\INI\\test.ini") // 섹션이름이나 키이름을 잘못사용 할 경우 "" 리턴하게 된다. 이런 경우는 코딩만 잘 한다면 없을 것임. // 읽기 public static string Read_Ini(string Section, string Key, int Size, string Path) { try { // string 읽기 StringBuilder gStringBuilder = new StringBuilder(Size); IniControl.GetPrivateProfileString(Section, Key, "", gStringBuilder, Size, Path); return gStringBuilder.ToString(); } catch { return null; } } // 라이트(예시) // OK // _Ini.Write_Ini("롯데", "이름", "자이언트", "C:\\Documents and Settings\\gadget81\\My Documents\\Visual Studio 2010\\Projects\\INI\\test.ini") // 해당 함수를 호출하게 되면 "롯데"섹션에 "이름"키에 "자이언트"를 라이팅하게 된다 // 라이트 public static void Write_Ini(string Section, string Key, string Value, string Path) { try { IniControl.WritePrivateProfileString(Section, Key, Value, Path); } catch { } } // 파일 생성 // Path는 "C:\\Documents and Settings\\gadget81\\My Documents\\Visual Studio 2010\\Projects\\INI\\test.ini" public static bool Create_Ini(string txt, string path) { try { // 파일이 존재하면 삭제 if (File.Exists(path)) { File.Delete(path); } // 파일 생성 File.WriteAllText(path, txt); // 성공 return true; } catch { // 실패 return false; } } // 파일 삭제 // Path는 "C:\\Documents and Settings\\gadget81\\My Documents\\Visual Studio 2010\\Projects\\INI\\test.ini" public static void Delete_Ini(string path) { try { // 파일이 존재하면 삭제 if (File.Exists(path)) { File.Delete(path); } } catch { } } } }