C# 创建INI文件,写入并可读取。
基于C#winform设计。
首先创建一个类,我命名为IniFiles。并引入命名空间using System.Runtime.InteropServices;
接着,声明API函数
[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函数方法
/// <summary> /// 写入INI文件 /// </summary> /// <param name="Section">项目名称(如 [TypeName] )</param> /// <param name="Key">键</param> /// <param name="Value">值</param> public void IniWriteValue(string Section, string Key, string Value) { WritePrivateProfileString(Section, Key, Value, this.inipath); }
读取INI文件方法
/// <summary> /// 读出INI文件 /// </summary> /// <param name="Section">项目名称(如 [TypeName] )</param> /// <param name="Key">键</param> public string IniReadValue(string Section, string Key) { StringBuilder temp = new StringBuilder(500); int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.inipath); return temp.ToString(); }
验证文件是否存在
/// <summary> /// 验证文件是否存在 /// </summary> /// <returns>布尔值</returns> public bool ExistINIFile() { return File.Exists(inipath); }
在其他窗体页面如何条用?请看:
这时候是创建INI文件(位置一般处于资源文件下bin\Debug目录)
public partial class Frm_Login : Form { HotelSystemORM.Unitl.IniFiles ini = new HotelSystemORM.Unitl.IniFiles(Application.StartupPath + @"\MyConfig.INI");//Application.StartupPath只适用于winform窗体程序 }
生成了文件之后就可以写入和读取信息了。
ini.IniWriteValue("登入详细", "账号", "test"); ini.IniWriteValue("登入详细", "密码", "password");
读取登入信息(页面加载的时候)
if (ini.ExistINIFile())//验证是否存在文件,存在就读取 label1.Text = ini.IniReadValue("登入详细", "用户名");
完成!
IniFiles类全部完整代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Runtime.InteropServices; namespace WindowsFormsApplication1 { public class IniFiles { public string inipath; //声明API函数 [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); /// <summary> /// 构造方法 /// </summary> /// <param name="INIPath">文件路径</param> public IniFiles(string INIPath) { inipath = INIPath; } public IniFiles() { } /// <summary> /// 写入INI文件 /// </summary> /// <param name="Section">项目名称(如 [TypeName] )</param> /// <param name="Key">键</param> /// <param name="Value">值</param> public void IniWriteValue(string Section, string Key, string Value) { WritePrivateProfileString(Section, Key, Value, this.inipath); } /// <summary> /// 读出INI文件 /// </summary> /// <param name="Section">项目名称(如 [TypeName] )</param> /// <param name="Key">键</param> public string IniReadValue(string Section, string Key) { StringBuilder temp = new StringBuilder(500); int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.inipath); return temp.ToString(); } /// <summary> /// 验证文件是否存在 /// </summary> /// <returns>布尔值</returns> public bool ExistINIFile() { return File.Exists(inipath); } } }
页面调用: