/******************************************************************
*
* ^_^ 恶猫 独门商标 挖哈哈
*
* QQ:\>23559055
*
* Site:\>http://emao.me
*
* ================================================================
*
* 说明:
*
* 1.命名空间: Emao_CfgManager
* 类名 : CfgManager
*
* 2.声明: CfgManager cm = new CfgManager();
*
* 3.具体方法自己看代码吧哈。简单的很。
*
* 分别是:add() readAll() writeAll()
*
******************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using System.Runtime.InteropServices; namespace Emao_CfgManager
{
public class CfgManager
{
private Dictionary<string ,string> dict= new Dictionary<string,string>();
private string filename = Application.StartupPath + @"\config.ini"; #region API
[DllImport("kernel32.dll")]
public extern static int GetPrivateProfileString(string segName, string keyName, string sDefault, byte[] bt, int nSize, string fileName);
[DllImport("kernel32.dll")]
public extern static int WritePrivateProfileString(string segName, string keyName, string sValue, string fileName); [DllImport("kernel32.dll")]
public extern static int GetPrivateProfileSection(string segName, StringBuilder buffer, int nSize, string fileName);
[DllImport("kernel32.dll")]
public extern static int WritePrivateProfileSection(string segName, string sValue, string fileName); #endregion #region constructure
public CfgManager()
{
ReadAll();
}
public CfgManager(string inipath)
{
filename = inipath;
ReadAll();
}
#endregion #region method
public int Add(object key, object value)
{
if (this.dict.ContainsKey(key.ToString()))
this.dict[key.ToString()] = value.ToString();
else
this.dict.Add(key.ToString(), value.ToString());
return this.dict.Count;
}
public void ReadAll()
{
this.dict.Clear();
//读取ini文件所有
StringBuilder sb = new StringBuilder();
Byte[] bt = new Byte[];
int len = GetPrivateProfileString("Config", null, "", bt, bt.GetUpperBound(), filename);
if (len != )
{
int start = ;
for (int i = ; i < len; i++)
{
if ((bt[i] == ) && ((i - start) > ))
{
String s = Encoding.GetEncoding().GetString(bt, start, i - start);
Byte[] temp = new Byte[];
int i2 = GetPrivateProfileString("Config", s, "", temp, , filename);
string value = System.Text.Encoding.Default.GetString(temp).Replace('\0', ' ');
this.dict.Add(s, value.TrimEnd());
start = i + ;
}
}
}
}
public void ReadAll(string sectionName)
{
this.dict.Clear();
//读取指定节点
}
public void WriteAll()
{
foreach (var dic in dict)
{
WritePrivateProfileString("Config",dic.Key,dic.Value,filename);
}
}
public string this[string key]
{
get
{
if (key.Trim() == "")
{
return "";
}
if (!this.dict.ContainsKey(key)) return "";
return this.dict[key];
}
set
{
if (!this.dict.ContainsKey(key))
this.dict.Add(key, value);
else
this.dict[key] = value;
}
}
#endregion #region Function #endregion #region property
public int Count
{
get { return this.dict.Count; }
}
#endregion }
}

原文:http://www.oschina.net/code/snippet_191468_13277

【转】ini载入保存类,操作INI配置文件方便的很的更多相关文章

  1. Properties类操作.properties配置文件方法总结

    一.properties文件 Properties文件是java中很常用的一种配置文件,文件后缀为“.properties”,属文本文件,文件的内容格式是“键=值”的格式,可以用“#”作为注释,jav ...

  2. VC++/MFC操作ini配置文件详解

    在我们写的程序当中,总有一些配置信息需要保存下来,以便完成程序的功能,最简单的办法就是将这些信息写入INI文件中,程序初始化时再读入.具体应用如下: 一.将信息写入.INI文件中. 1.所用的WINA ...

  3. android操作ini工具类

    package com.smarteye.common; import java.io.BufferedReader; import java.io.BufferedWriter; import ja ...

  4. Python中操作ini配置文件

    这篇博客我主要想总结一下python中的ini文件的使用,最近在写python操作mysql数据库,那么作为测试人员测试的环境包括(测试环境,UAT环境,生产环境)每次需要连接数据库的ip,端口,都会 ...

  5. php操作ini配置文件

    有些配置化的数据放到配置文件可以方便管理,比如数据库信息,路由信息,先建立配置文件,test.ini [database_setting] host=127.0.0.1 user=root passw ...

  6. C# 如何实现完整的INI文件读写类

    作者: 魔法软糖 日期: 2020-02-27 引言 ************************************* .ini 文件是Initialization File的缩写,即配置文 ...

  7. [转]C#操作INI文件

    在很多的程序中,我们都会看到有以.ini为后缀名的文件,这个文件可以很方便的对程序配置的一些信息进行设置和读取,比如说我们在做一个程序后台登陆的时候,需要自动登录或者是远程配置数据库连接,及保存密码设 ...

  8. C#操作INI文件(明天陪你看海)

    C#操作INI文件 在很多的程序中,我们都会看到有以.ini为后缀名的文件,这个文件可以很方便的对程序配置的一些信息进行设置和读取,比如说我们在做一个程序后台登陆的时候,需要自动登录或者是远程配置数据 ...

  9. Ini文件帮助类

    .ini文件是什么 .ini 文件是Initialization File的缩写,就是初始化文件.在Windows系统中,其是配置文件所采用的存储格式(主要是system.ini,win.ini,sy ...

随机推荐

  1. asp.net mvc Post上传文件大小限制

    最近发现在项目中使用jQuery.form插件上传比较大的文件时,上传不了,于是改了下web.config的上传文件最大限制. <configuration> <system.web ...

  2. c语言.大数的输出

    转化成字符串,再用for循环输出: #include <stdio.h>#include <string.h>int main(){  char s[32];   int d, ...

  3. 20145337《Java程序设计》第三周学习总结

    20145337 <Java程序设计>第三周学习总结 教材学习内容总结 类与对象 类与对象的关系:要产生对象必须先定义类,类是对象的设计图,对象是类的实例.我觉得在视频中对类与对象关系的描 ...

  4. ServletConfig对象和它在开发中的应用场

    package cn.itcast; import java.io.IOException; import java.io.PrintWriter; import java.util.Enumerat ...

  5. 节点操作-创建并添加&删除节点&替换&克隆节点

    <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" ...

  6. C#网络编程之--TCP协议(一)

    TCP 是面向连接的传输协议 面向连接,其实就好比,A打电话给B,如果B接听了,那么A和B之间就的通话,就是面向连接的 TCP 是全双工的传输协议 全双工,这个理解起来也很简单,A打电话给B,B接听电 ...

  7. Linux系统下安装MongoDB 指南

    1.首先连接到Linux系统. 2.到MongoDB官网下载合适的MongoDB安装压缩包. 下载页面:https://www.mongodb.org/downloads#production. 这里 ...

  8. http://bbs.tianya.cn/post-stocks-1665898-1.shtml

    http://bbs.tianya.cn/post-stocks-1295707-1.shtml 写过交易计划吗?有没有提前关注过某些板块或者某些股票呢?自选股里边有多少只股票?你平时复盘都是什么习惯 ...

  9. python Shapely 使用指南

    翻译:http://toblerity.org/shapely/manual.html 引入包 from shapely.geometry import Point from shapely.geom ...

  10. 修改git commit默认触发的编辑器

    使用git commit会自动启动编辑器编辑本次提交的comment,git默认配置是GNU nano,但是很多人不是很熟悉(我就是),其实这个是可以自行配置的. 编辑制定代码库的.git/confi ...