본문 바로가기
프로그래밍 언어/C#

[C#] .ini 파일의 개념과 사용 방법

by mssil-7 2024. 7. 24.

.ini 파일의 개념과 GetPrivateProfileString, WritePrivateProfileString 사용 방법 알아보기

 

.ini 파일

속성을 구성하는 특성 및 섹션에 대한 공개 키를 포함하는 컴퓨터 프로그램에 대한 메시지 구성 문서 입니다. 

.ini 파일은 쉽게는 메모장으로 열 수 있으며 initialization 을 의미합니다.

 

 

 

ini 파일의 형식은 이와 같습니다.

 

INIFile.ini

--------------------------------------------------------------------------

[영역의 이름]

키=값

키=값

--------------------------------------------------------------------------

 

 

 

 

 

< 전체 코드 >

    class Program
    {
        [DllImport("kernel32")]
        public static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32")]
        public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);


        public static string IniRead(string Section, string Key, string sDefault, string path)
        {
            // Section에 해당하는 key의 value값이 없을 경우 key를 생성하고 그 값을 sDefault 값으로 저장합니다.
            StringBuilder temp = new StringBuilder(4000);
            try
            {
                int i = GetPrivateProfileString(Section, Key, "", temp, 4000, path);
                if (i == 0)
                {
                    IniWrite(Section, Key, sDefault, path);
                    GetPrivateProfileString(Section, Key, "", temp, 4000, path);

                    if (temp.ToString() == "") { return sDefault; }
                }
            }
            catch (Exception ex)
            {
            }
            return temp.ToString();
        }

        public static void IniWrite(string Section, string Key, string Value, string path)
        {
            try
            {
                WritePrivateProfileString(Section, Key, Value, path);
            }
            catch (Exception ex)
            {
            }
        }



        static void Main(string[] args)
        {
            string PATH = @"C:\Users\user\Desktop";
            string INIFILE = Path.Combine(PATH, "INIFile.ini");
            
            // .ini 파일 쓰기
            IniWrite("Section", "key1", "value1", INIFILE);
            IniWrite("Section", "key2", "value2", INIFILE);
            IniWrite("Section", "key3", "value3", INIFILE);

            // .ini 파일 읽기
            string v1 = IniRead("Section", "key1", "value", INIFILE);
            string v2 = IniRead("Section", "key2", "value", INIFILE);

            Console.WriteLine("Section key1 : " + v1);
            Console.WriteLine("Section key2 : " + v2);

            // 만약에 해당하는 key가 없을 경우 예시
            string v4 = IniRead("Section", "key4", "value", INIFILE);
            Console.WriteLine("Section key4 : " + v4);
        }
    }

 

 

실행 결과

 

 

< 참고 문서 >

https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getprivateprofilestring

 

GetPrivateProfileString function (winbase.h) - Win32 apps

The GetPrivateProfileString function (winbase.h) retrieves a string from the specified section in an initialization file.

learn.microsoft.com

https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-writeprivateprofilestringa

 

WritePrivateProfileStringA function (winbase.h) - Win32 apps

Copies a string into the specified section of an initialization file. (ANSI)

learn.microsoft.com