123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Runtime.InteropServices;
- using System.IO;
- namespace FPER
- {
- 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 //(Exception e)
- {
- // LOG
- ////_Event.DebugView_SendMessage_Write(e.ToString());(e.ToString());
- // 실패
- 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
- {
- }
- }
- }
- }
|