언어/C#

[ini] ini파일 읽기 쓰기 기본 코드

까망후니 2017. 2. 14. 09:19

using System.Runtime.InteropServices;       


        [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);


        private string FilePath = AppDomain.CurrentDomain.BaseDirectory + "init.ini";


// ini 파일에 쓰기

private void WriteIni(string section, string key, string value)

        {

            WritePrivateProfileString(section, key, value, FilePath);

        }


        // ini파일에서 읽기

        private string Readini(string section, string key)

        {

            StringBuilder temp = new StringBuilder(255);

            int ret = GetPrivateProfileString(section, key, "", temp, 255, FilePath);            

            return temp.ToString();

        }




         textBox.Text = Readini("Section 값", "Key 값"); //이렇게 읽고

         WriteIni("Section 값", "Key 값", textBox.Text); //이렇게 쓴다.