首页 > 代码库 > C# 读取和配置IniFile
C# 读取和配置IniFile
自定义IniFile操作类
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runtime.InteropServices;using System.IO;namespace Sisley{ public class INIFileHepler { private string Default_IniFileName = "Config.ini"; //INI文件名 private string strIniFilePath; // 返回0表示失败,非0为成功 [DllImport("kernel32", CharSet = CharSet.Auto)] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); // 返回取得字符串缓冲区的长度 [DllImport("kernel32", CharSet = CharSet.Auto)] private static extern long GetPrivateProfileString(string section, string key, string strDefault, StringBuilder retVal, int size, string filePath); /* [DllImport("Kernel32.dll", CharSet = CharSet.Auto)] public static extern int GetPrivateProfileInt(string section, string key, int nDefault, string filePath); */ public INIFileHepler() { this.strIniFilePath = Directory.GetCurrentDirectory() + "\\" +Default_IniFileName; } public INIFileHepler(string strIniFilePath) { if ( Directory.Exists(strIniFilePath)) { this.strIniFilePath = Directory.GetCurrentDirectory() + "\\" + Default_IniFileName; } this.strIniFilePath = strIniFilePath; } /// <summary> /// 写入 /// </summary> /// <param name="section">写入section</param> /// <param name="key">写入key</param> /// <param name="value">写入value</param> public void WriteIniString(string section, string key, string value) { try { WritePrivateProfileString(section, key, value, strIniFilePath); } catch { throw new Exception("配置文件不存在或权限不足导致无法写入"); } } /// <summary> /// 获取ini配置文件中的字符串 /// </summary> /// <param name="section">节名</param> /// <param name="key">键名</param> /// <param name="strDefault">默认值</param> /// <param name="retVal">结果缓冲区</param> /// <param name="size">结果缓冲区大小</param> /// <returns>返回值</returns> public string GetIniString(string section, string key, string strDefault, StringBuilder retVal, int size) { long liRet = GetPrivateProfileString(section, key, strDefault, retVal, size, strIniFilePath); //return (liRet >= 1); StringBuilder temp = new StringBuilder(1024); INIFileHepler.GetPrivateProfileString(section, key, strDefault, temp, 1024, strIniFilePath); return temp.ToString(); } }}
使用示例:
设置和设置
INIFileHepler inifile = new INIFileHepler(); //INIFileHepler inifile = new INIFileHepler(@"D:\MyDocs\CSharp\Program\bin\Debug\TestConfig.ini"); private void button1_Click(object sender, EventArgs e) { inifile.WriteIniString("DatabaseConfig", "Server", "192.168.10.3"); inifile.WriteIniString("DatabaseConfig", "DBName", "DB_Live"); inifile.WriteIniString("DatabaseConfig", "UserName", "admin"); inifile.WriteIniString("DatabaseConfig", "UserPassword", "123456"); } private void button2_Click(object sender, EventArgs e) { richTextBox1.AppendText("DatabaseConfig"+"\n"); StringBuilder Server = new StringBuilder(32); StringBuilder DBName = new StringBuilder(32); StringBuilder UserName = new StringBuilder(32); StringBuilder UserPassword = new StringBuilder(32); inifile.GetIniString("DatabaseConfig", "Server", "", Server, Server.Capacity); inifile.GetIniString("DatabaseConfig", "DBName", "", DBName, DBName.Capacity); inifile.GetIniString("DatabaseConfig", "UserName", "", UserName, UserName.Capacity); inifile.GetIniString("DatabaseConfig", "UserPassword", "", UserPassword, UserPassword.Capacity); richTextBox1.AppendText("Server: " +Server+ "\n"); richTextBox1.AppendText("DBName: " + DBName + "\n"); richTextBox1.AppendText("UserName: " + UserName + "\n"); richTextBox1.AppendText("UserPassword: " + UserPassword + "\n"); }
C# 读取和配置IniFile
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。