舞步者

天行健,君子以自强不息;地势坤,君子以厚德载物
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C#调用API读写INI文件

Posted on 2006-04-18 14:38  舞步者  阅读(192)  评论(0)    收藏  举报

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        [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);
        //对INI文件写操作
        public void IniWriteValue(string Section, string Key, string Value, string filepath)
        {
            WritePrivateProfileString(Section, Key, Value, filepath);
        }
        public Form1()
        {
            InitializeComponent();
        }
        //对INI文件读操作
        public string IniReadValue(string Section, string Key, string filepath)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, filepath);
            return temp.ToString();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        //读取INI文件
        private void button1_Click(object sender, EventArgs e)
        {
            string path1 = Directory.GetCurrentDirectory().ToString();
            this.textBox1.Text = IniReadValue("ODBC 32 bit Data Source", "MS Access Database", path1 + "\\555.ini");
        }
        //写入INI文件
        private void button2_Click(object sender, EventArgs e)
        {
            string path1 = Directory.GetCurrentDirectory().ToString();
            IniWriteValue("ODBC 32 bit Data Source", "MS Access Database", this.textBox1.Text, path1+"\\555.ini");
            MessageBox.Show(path1);
        }
    }
}