读写ini文件:C#读写ini文件
来源: 发布时间:星期五, 2009年1月9日 浏览:18次 评论:0
主要思路是 Win32 API 1.引入命名空间 using .Runtime.InteropServices; 2.声明(把 个Win32 API 转成C# ) //声明INI文件写操作 WritePrivateProfileString [DllImport("kernel32")] private extern long WritePrivateProfileString( section, key, val, filePath); //声明INI文件读操作 GetPrivateProfileString [DllImport("kernel32")] private extern GetPrivateProfileString( section, key, def, StringBuilder retVal, size, filePath); 3. //写个config.ini文件 private void Save_Ini { s = .Windows.Forms.Application.ExecutablePath; //在当前目录下写个config.ini文件 path = s.ToLower.Replace("mediaconvert.exe", "config.ini"); configureNode = "DataBaseConfigure";//配置节 key1 = "DataBase";//键名 key1_Value = "DataBaseName";//键值 key2 = "Server"; key2_Value = "ServerName"; key3 = "UserId"; key3_Value = "1"; WritePrivateProfileString(configureNode, key1, key1_Value, path); WritePrivateProfileString(configureNode, key2, key2_Value, path); WritePrivateProfileString(configureNode, key3, key3_Value, path); /*最后在exe文件同目录下生成个config.ini文件内容应如下: * [DataBaseConfigure] * DataBase=DataBaseName * Server=ServerName * UserId=1 */ } //读取config.ini文件中配置 private void Read_Ini { s = .Windows.Forms.Application.ExecutablePath; //取得config.ini路径 path = s.ToLower.Replace("mediaconvert.exe", "config.ini"); StringBuilder str = StringBuilder(255); //取得配置节[DataBaseConfigure]DataBase键值 GetPrivateProfileString("DataBaseConfigure", "DataBase", "", str, 255, path); //对话框中结果应该为 DataBase:DataBaseName .Windows.Forms.MessageBox.Show("DataBase:" + str.); } C#使用系统Api 最头疼 问题就是Api中 数据类型在C#中 如何对应 问题 这个网站WebSitehttp://www.pinvoke.net/列出了大多系统Api在此C#或VB.Net中 对应声明 很详细
相关文章
读者评论
发表评论
|
|