【转】ini载入保存类,操作INI配置文件方便的很
/******************************************************************
*
* ^_^ 恶猫 独门商标 挖哈哈
*
* 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配置文件方便的很的更多相关文章
- Properties类操作.properties配置文件方法总结
一.properties文件 Properties文件是java中很常用的一种配置文件,文件后缀为“.properties”,属文本文件,文件的内容格式是“键=值”的格式,可以用“#”作为注释,jav ...
- VC++/MFC操作ini配置文件详解
在我们写的程序当中,总有一些配置信息需要保存下来,以便完成程序的功能,最简单的办法就是将这些信息写入INI文件中,程序初始化时再读入.具体应用如下: 一.将信息写入.INI文件中. 1.所用的WINA ...
- android操作ini工具类
package com.smarteye.common; import java.io.BufferedReader; import java.io.BufferedWriter; import ja ...
- Python中操作ini配置文件
这篇博客我主要想总结一下python中的ini文件的使用,最近在写python操作mysql数据库,那么作为测试人员测试的环境包括(测试环境,UAT环境,生产环境)每次需要连接数据库的ip,端口,都会 ...
- php操作ini配置文件
有些配置化的数据放到配置文件可以方便管理,比如数据库信息,路由信息,先建立配置文件,test.ini [database_setting] host=127.0.0.1 user=root passw ...
- C# 如何实现完整的INI文件读写类
作者: 魔法软糖 日期: 2020-02-27 引言 ************************************* .ini 文件是Initialization File的缩写,即配置文 ...
- [转]C#操作INI文件
在很多的程序中,我们都会看到有以.ini为后缀名的文件,这个文件可以很方便的对程序配置的一些信息进行设置和读取,比如说我们在做一个程序后台登陆的时候,需要自动登录或者是远程配置数据库连接,及保存密码设 ...
- C#操作INI文件(明天陪你看海)
C#操作INI文件 在很多的程序中,我们都会看到有以.ini为后缀名的文件,这个文件可以很方便的对程序配置的一些信息进行设置和读取,比如说我们在做一个程序后台登陆的时候,需要自动登录或者是远程配置数据 ...
- Ini文件帮助类
.ini文件是什么 .ini 文件是Initialization File的缩写,就是初始化文件.在Windows系统中,其是配置文件所采用的存储格式(主要是system.ini,win.ini,sy ...
随机推荐
- 自动解析URL
function parseURL(url) { var a = document.createElement('a'); a.href = url; return { source: url, pr ...
- 5_STL设计理念_迭代器
他山之石,可以攻玉. http://blog.csdn.net/jxh_123/article/details/30793397?utm_source=tuicool&utm_medium=r ...
- 学Python后到底能干什么?
Python是一种什么语言? Python是一种计算机程序设计语言.你可能已经听说过很多种流行的编程语言,比如非常难学的C语言,非常流行的Java语言,适合初学者的Basic语言,适合网页编程的Jav ...
- unicode可以通过编码(encode)成为特定编码的str
1.原始字符串python中的原始字符串以r开头,使用原始字符串可以避免字符串中转义字符带来的问题,例如写路径时 path = 'c:\noway',此时用 print path,其结果为:c:owa ...
- Android课程---qq登陆页面(练习)
AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xm ...
- IOS第16天(5,Quartz2D雪花)
*** #import "HMView.h" @interface HMView() { int count; } @property (nonatomic, assign) CG ...
- Thinking in Java——笔记(4)
Controlling Execution true and false Java doesn't allow you to use a number as a boolean. If you wan ...
- PHP- 数字转汉字
//数字转汉字 function number2Chinese($num, $m = 1) { switch($m) { case 0: $CNum = array( array('零','壹','贰 ...
- 在eclipse中将SVN项目check下来的正确步骤
学习下面的方法后再也不用从svncheck到本地后再导入到eclipse里了. 1. 首先Import,在弹出框里选择SVN-从SVN检出项目,然后按照提示一步一步直到选中了目标项目,然后点击next ...
- Day12~13(2016/2/1~2/2)
进度:实现了自定义标题栏控件并调用