INI(Initialization) 파일 포맷은 설정 파일에 대한 사실상 표준이다. INI 파일은 단순 구조의 텍스트 파일로 이루어져 있다. 보통 마이크로소프트 윈도우와 연결되어 있지만 다른 운영 체제에서도 사용할 수 있다. "INI 파일"이라는 이름은 ".INI"라는 파일 확장자가 따라오지만, ".CFG", ".conf", ".TXT" 등의 다른 확장자를 사용하기도 한다.(위키백과)
// ini 파일을 불러올 때 사용되는 함수
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);
// ini 파일에 저장할 때 사용되는 함수
[DllImport("kernel32.dll")]
private static extern long WritePrivateProfileString(String section, String key, String val, String filePath);
// INI 값을 읽어 온다.
private String GetIniValue(String Section, String Key)
{
FileInfo exefileinfo = new FileInfo(Application.ExecutablePath);
string path = exefileinfo.Directory.FullName.ToString() //프로그램 실행되고 있는데 path 가져오기
string iniPath = @”\chatgpt.ini”; //파일명
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, “”, temp, 255, iniPath);
return temp.ToString();
}
// INI 값을 셋팅
private void SetIniValue(String Section, String Key, String Value)
{
FileInfo exefileinfo = new FileInfo(Application.ExecutablePath);
string path = exefileinfo.Directory.FullName.ToString() //프로그램 실행되고 있는데 path 가져오기
string iniPath = @”\chatgpt.ini”; //파일명
WritePrivateProfileString(Section, Key, Value, iniPath);
}
// ini에서 key값 가져오기
string sApiKey = "";
sApiKey = GetIniValue("Setting", "apikey");
// ini에서 key값 저장하기
string sApiKey = "12345";
SetIniValue("Setting", "apikey", sApiKey);
'공학속으로 > C#' 카테고리의 다른 글
C# 리스트박스(ListBox) 사용법 (0) | 2023.07.07 |
---|---|
[C#] 파일 폴더 드래그앤드롭 (0) | 2023.07.07 |
C# String.Format - 3자리 마다 콤마 등 서식문자열, 자릿수 표시하기 (0) | 2023.04.11 |
C# 문자열 연결하는 4가지 방법 (0) | 2023.04.11 |
C# 문자열에 특정 문자열 포함 여부 확인하기 (0) | 2023.04.11 |
댓글