/******************************************************************
*
* ^_^ 恶猫 独门商标 挖哈哈
*
* 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. Fail to start neutron-server

    问题: [root@localhost ~]# systemctl status neutron-server ● neutron-server.service - OpenStack Neutron ...

  2. mac El Capitan 10.11.6

    http://bbs.pcbeta.com/forum.php?mod=viewthread&tid=1702502&authorid=4532202&page=1

  3. support HTML5 in IE8

    <!--this code is used to support HTML5 in IE8--> <!--[if IE]><script type="text/ ...

  4. Nest查询示例

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. Visual Studio中编写C程序

    相信很多科班出身的程序猿和我一样,第一个接触到的编程语言是C语言,第一个写的程序是“Hello World!”. 对于一个.Net程序猿,VS肯定是个非常熟悉的工具,但是如何使用VS编写一个C语言程序 ...

  6. ConcurrentHashMap Put()操作示例代码

    非常简练: private static void put(MetricKey key, float value) { MetricValue current; do { current = map. ...

  7. PHP调用内容DES加密的SOAP接口

    本文以方倍工作室优惠券接口开发为例,介绍PHP下DES加解密及SOAP接口调用的实现过程. 一.基础概念 DES全称为Data Encryption Standard,即数据加密标准,是一种使用密钥加 ...

  8. Vcenter server 5.5上传ISO镜像

    1.摘要 --- 浏览要上传的硬盘. 2.创建一个文件夹专业门存放ISO镜像. 3.选择上传文件. 4.选择文件上传即可.

  9. 微软自带的Serialization和Newtonsoft简单测试

    刚刚对这两个进行了一下小小的测试 发现 当转换的内容少的时候  微软自带的比Newtonsoft速度要快一些,内容多的时候反之,当内容多到一定量的时候微软自带的就不能转换了,需要修改一下MaxJson ...

  10. .Net分布式架构(一):Nginx实现负载均衡

    一:负载均衡简介 负载均衡,英文名称为Load Balance,其意思就是分摊到多个操作单元上进行执行,例如Web服务器.FTP服务器.企业关键应用服务器和其它关键任务服务器等,从而共同完成工作任务. ...