[C#] WritePrivateProfileString, GetPrivateProfileString in C#
2009. 2. 26. 21:53ㆍCoders
출처 : System.Runtime.InteropServices.DllImport
C# 에서 ini 파일에 WriteProfile..., GetProfile... 함수를 사용하는 방법입니다. VC++ 에서는 간단하게 App에서 m_pszProfileName 멤버에 ini 경로를 넣어주면 됐는데, 골치아프죠? 제가 찾았던 건, VC++ 의 해당 함수를 대체하는 C# 코드였는데, 이상한 것만 찾았습니다. ㅠㅠ
//사용법은 간단합니다.-초기화(굵은부분은 현재 실행 디렉토리를 가져오는 부분)
IniFile ini = new IniFile(string.Format("{0}\\MyIni.ini", System.Environment.CurrentDirectory));
//읽을 때
string sMyValue = ini.ReadValue("MyProgram", "MySection");
//쓸 때
ini.WriteValue("MyProgram", "MySection", sMyValue);
C# 에서 ini 파일에 WriteProfile..., GetProfile... 함수를 사용하는 방법입니다. VC++ 에서는 간단하게 App에서 m_pszProfileName 멤버에 ini 경로를 넣어주면 됐는데, 골치아프죠? 제가 찾았던 건, VC++ 의 해당 함수를 대체하는 C# 코드였는데, 이상한 것만 찾았습니다. ㅠㅠ
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace with_soju.tistory.com
- {
- class IniFile
- {
- private string path;
- [System.Runtime.InteropServices.DllImport("kernel32")]
- private static extern long WritePrivateProfileString(
- string section, string key, string val, string filePath);
- [System.Runtime.InteropServices.DllImport("kernel32")]
- private static extern int GetPrivateProfileString(
- string section, string key, string def,
- StringBuilder retVal, int size, string filePath);
- public IniFile(string INIPath)
- {
- path = INIPath;
- }
- public void WriteValue(string Section, string Key, string Value)
- {
- WritePrivateProfileString(Section, Key, Value, this.path);
- }
- public string ReadValue(string Section, string Key)
- {
- int i = GetPrivateProfileString(
- Section, Key, string.Empty, temp, 255, this.path);
- return temp.ToString();
- }
- }
- }
//사용법은 간단합니다.-초기화(굵은부분은 현재 실행 디렉토리를 가져오는 부분)
IniFile ini = new IniFile(string.Format("{0}\\MyIni.ini", System.Environment.CurrentDirectory));
//읽을 때
string sMyValue = ini.ReadValue("MyProgram", "MySection");
//쓸 때
ini.WriteValue("MyProgram", "MySection", sMyValue);
'Coders' 카테고리의 다른 글
티맥스윈도의 DBMS 탑재 (4) | 2009.07.29 |
---|---|
[C#] String.Format(); 메소드에서... (0) | 2009.02.25 |
[MSSQL] ROLLUP 을 이용한 소계/총계 - GROUPING 함수 (4) | 2009.02.19 |