전체소스
C#에서는 ini 파일을 쉽게 다룰수 있습니다.
kernel32 파일에 있는 WritePrivateProfileString 와 GetPrivateProfileString 함수를 이용하면 간단합니다.
using System.Runtime.InteropServices; 를 선언해 주어야 합니다.
함수 사용방법
WritePrivateProfileString(string section, string key, string val, string filePath);
- setcion : 섹션값
- key : 키값
- val : 데이터 값
- filePath : ini 파일의 경로
GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
- setcion : 섹션값
- key : 키값
- def: default 데이터값
- retVal : 가져올 데이터 값
- size : 데이터 크기
- filePath : ini 파일의 경로
string m_strSection = "wat_ini_section";
string m_strPath = System.Environment.CurrentDirectory+"\\wat_ini.ini";
// kernel32 파일에서 WritePrivateProfileString 와 GetPrivateProfileString
// 를 사용하기 위한 선언
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
public frmMain()
{
InitializeComponent();
txtName1.Text = GetProfileString(m_strSection, "txtName1", string.Empty, 255, m_strPath);
txtAge1.Text = GetProfileString(m_strSection, "txtAge1", string.Empty, 255, m_strPath);
txtAddr1.Text = GetProfileString(m_strSection, "txtAddr1", string.Empty, 255, m_strPath);
txtName2.Text = GetProfileString(m_strSection, "txtName2", string.Empty, 255, m_strPath);
txtAge2.Text = GetProfileString(m_strSection, "txtAge2", string.Empty, 255, m_strPath);
txtAddr2.Text = GetProfileString(m_strSection, "txtAddr2", string.Empty, 255, m_strPath);
}
private void txtName1_TextChanged(object sender, EventArgs e)
{
WritePrivateProfileString(m_strSection, "txtName1", txtName1.Text, m_strPath);
}
private void txtAge1_TextChanged(object sender, EventArgs e)
{
WritePrivateProfileString(m_strSection, "txtAge1", txtAge1.Text, m_strPath);
}
private void txtAddr1_TextChanged(object sender, EventArgs e)
{
WritePrivateProfileString(m_strSection, "txtAddr1", txtAddr1.Text, m_strPath);
}
private void txtName2_TextChanged(object sender, EventArgs e)
{
WritePrivateProfileString(m_strSection, "txtName2", txtName2.Text, m_strPath);
}
private void txtAge2_TextChanged(object sender, EventArgs e)
{
WritePrivateProfileString(m_strSection, "txtAge2", txtAge2.Text, m_strPath);
}
private void txtAddr2_TextChanged(object sender, EventArgs e)
{
WritePrivateProfileString(m_strSection, "txtAddr2", txtAddr2.Text, m_strPath);
}
private string GetProfileString(string section, string key, string def, int size, string filePath)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(section, key, def, temp, size, filePath);
return temp.ToString();
}
관련 문서가 검색되었습니다.
- [2018/02/26] List 에서 고유값 얻기 ()
- [2015/05/22] C#, 아두이노 간의 WIFI 통신으로 LCD 제어 (4642)
- [2015/04/22] 시리얼통신 소스 코드입니다. 0.2 (12264) *2
- [2015/03/13] 시리얼통신 소스 코드입니다. 0.1 (9881)
- [2015/03/13] 항상 마지막에 추가한 TEXT 보이게 ()
- [2014/01/17] ComboBox Text 편집 안되게 (15451)
- [2014/01/08] if 문에서 여러개 비교할때 (26020) *3
- [2013/12/30] C++, C# 간단한 기능 비교 (13742)
- [2013/12/18] 3자리마다 ,(콤마) 찍기 (원화, 달러 표시) (15874)
- [2011/10/27] WAT-AVR128 모듈 (5948)

