读写ini

string file = Application.StartupPath + "\config.ini";
        public Form1()
        {
            InitializeComponent();
        }
        [DllImport("kernel32")]
        private static extern bool WritePrivateProfileString(string section, string key, string val, string filePath);

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);
        private void button1_Click(object sender, EventArgs e)
        {
            string s = ReadINI("config", "user");
            label1.Text = s;
        }

        private void button2_Click(object sender, EventArgs e)
        {            
            WritePrivateProfileString("config", "user", "lake.liu", file);
            WritePrivateProfileString("config", "name", "lake", file);
            WritePrivateProfileString("config", "age", "40", file);
        }

        public string ReadINI(string section, string key)
        {
            byte[] Buffer = new byte[128];
            int bufLen = GetPrivateProfileString(section, key, "", Buffer, Buffer.GetUpperBound(0), file);
            string s = Encoding.GetEncoding(0).GetString(Buffer);
            s = s.Substring(0, bufLen);
            return s.Trim();
        }

读写ini 代码参考

原文地址:https://www.cnblogs.com/lakeliu/p/11969986.html