[轉貼] 讀寫 INI檔 Win32 API

2012111916:48

出處:
http://social.msdn.microsoft.com/Forums/zh-TW/233/thread/a3f15a39-c021-4f20-a120-a783a36b67ca

http://www.dotblogs.com.tw/chiajung/archive/2009/11/05/11437.aspx

        [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
        static extern uint GetPrivateProfileString(
           string lpAppName,
           string lpKeyName,
           string lpDefault,
           StringBuilder lpReturnedString,
           uint nSize,
           string lpFileName);

        [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool WritePrivateProfileString(string lpAppName,
           string lpKeyName, string lpString, string lpFileName);
       
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //讀取ini檔
            StringBuilder sb = new StringBuilder(500);
            uint res = GetPrivateProfileString("AppName", "KeyName", "", sb, (uint)sb.Capacity, @"c:\test.ini");
            MessageBox.Show(sb.ToString());

            //寫入ini檔
            WritePrivateProfileString("AppName", "KeyName", "oooo", @"c:\test.ini");
        }