C# 配置文件ini操作类
// [ DllImport ( "kernel32" ) ]
//private static extern long WritePrivateProfileString ( string section , string key , string val , string filePath ) ;
//参数说明:section:INI文件中的段落;key:INI文件中的关键字;val:INI文件中关键字的数值;filePath:INI文件的完整的路径和名称。
//C#申明INI文件的读操作函数GetPrivateProfileString():
//[ DllImport ( "kernel32" ) ]
//private static extern int GetPrivateProfileString ( string section , string key , string def , StringBuilder retVal , int size , string filePath ) ;
//参数说明:section:INI文件中的段落名称;key:INI文件中的关键字;
//def:无法读取时候时候的缺省数值;retVal:读取数值;size:数值的大小;filePath:INI文件的完整路径和名称。
// //第一种,简单操作类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO; namespace ConsoleApplication1
{
class INIhelp
{
[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文件名称
private static string inifilename = "Config.ini";
//获取ini文件路径
private static string inifilepath = Directory.GetCurrentDirectory() + "\\" + inifilename; public static string GetValue(string key)
{
StringBuilder s = new StringBuilder();
GetPrivateProfileString("CONFIG",key,"",s,,inifilepath);
return s.ToString();
} public static void SetValue(string key,string value)
{
try
{
WritePrivateProfileString("CONFIG", key, value, inifilepath);
}
catch (Exception ex)
{
throw ex;
}
}
}
} // 使用
static void Main(string[] args)
{
INIhelp.SetValue("data", "abcdefg");
INIhelp.SetValue("哈哈", "");
INIhelp.SetValue("呵呵", "");
INIhelp.SetValue("数据库", "");
INIhelp.SetValue("", "");
Console.WriteLine("写入完成");
Console.ReadLine(); string s = INIhelp.GetValue("data");
Console.WriteLine(s);
string a = INIhelp.GetValue("哈哈");
Console.WriteLine(a);
string b = INIhelp.GetValue("");
Console.WriteLine(b);
Console.ReadLine();
//删除,设置为null即可
INIhelp.SetValue("1", null);
} //第二种 完全操作类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text; namespace MSFramework.Common
{
/// <summary>
/// ini文件类
/// </summary>
public class IniFile
{
private string m_FileName; public string FileName
{
get { return m_FileName; }
set { m_FileName = value; }
} [DllImport("kernel32.dll")]
private static extern int GetPrivateProfileInt(
string lpAppName,
string lpKeyName,
int nDefault,
string lpFileName
); [DllImport("kernel32.dll")]
private static extern int GetPrivateProfileString(
string lpAppName,
string lpKeyName,
string lpDefault,
StringBuilder lpReturnedString,
int nSize,
string lpFileName
); [DllImport("kernel32.dll")]
private static extern int WritePrivateProfileString(
string lpAppName,
string lpKeyName,
string lpString,
string lpFileName
); /// <summary>
/// 构造函数
/// </summary>
/// <param name="aFileName">Ini文件路径</param>
public IniFile(string aFileName)
{
this.m_FileName = aFileName;
} /// <summary>
/// 构造函数
/// </summary>
public IniFile()
{ } /// <summary>
/// [扩展]读Int数值
/// </summary>
/// <param name="section">节</param>
/// <param name="name">键</param>
/// <param name="def">默认值</param>
/// <returns></returns>
public int ReadInt(string section, string name, int def)
{
return GetPrivateProfileInt(section, name, def, this.m_FileName);
} /// <summary>
/// [扩展]读取string字符串
/// </summary>
/// <param name="section">节</param>
/// <param name="name">键</param>
/// <param name="def">默认值</param>
/// <returns></returns>
public string ReadString(string section, string name, string def)
{
StringBuilder vRetSb = new StringBuilder();
GetPrivateProfileString(section, name, def, vRetSb, , this.m_FileName);
return vRetSb.ToString();
} /// <summary>
/// [扩展]写入Int数值,如果不存在 节-键,则会自动创建
/// </summary>
/// <param name="section">节</param>
/// <param name="name">键</param>
/// <param name="Ival">写入值</param>
public void WriteInt(string section, string name, int Ival)
{ WritePrivateProfileString(section, name, Ival.ToString(), this.m_FileName);
} /// <summary>
/// [扩展]写入String字符串,如果不存在 节-键,则会自动创建
/// </summary>
/// <param name="section">节</param>
/// <param name="name">键</param>
/// <param name="strVal">写入值</param>
public void WriteString(string section, string name, string strVal)
{
WritePrivateProfileString(section, name, strVal, this.m_FileName);
} /// <summary>
/// 删除指定的 节
/// </summary>
/// <param name="section"></param>
public void DeleteSection(string section)
{
WritePrivateProfileString(section, null, null, this.m_FileName);
} /// <summary>
/// 删除全部 节
/// </summary>
public void DeleteAllSection()
{
WritePrivateProfileString(null, null, null, this.m_FileName);
} /// <summary>
/// 读取指定 节-键 的值
/// </summary>
/// <param name="section"></param>
/// <param name="name"></param>
/// <returns></returns>
public string IniReadValue(string section, string name)
{
StringBuilder strSb = new StringBuilder();
GetPrivateProfileString(section, name, "", strSb, , this.m_FileName);
return strSb.ToString();
} /// <summary>
/// 写入指定值,如果不存在 节-键,则会自动创建
/// </summary>
/// <param name="section"></param>
/// <param name="name"></param>
/// <param name="value"></param>
public void IniWriteValue(string section, string name, string value)
{
WritePrivateProfileString(section, name, value, this.m_FileName);
}
}
} //使用
IniFile myIni = new IniFile(Environment.CurrentDirectory + "\\Config.ini");
myIni.WriteInt("Tsection", "tName", );
Console.WriteLine(myIni.ReadString("System", "MainFormName", ""));
Console.WriteLine(myIni.ReadString("DbServer", "DbFile", ""));
myIni.WriteString("DbServer", "DbFile", @"E:\Work\test2.db");
Console.WriteLine(myIni.ReadString("DbServer", "DbServer", ""));
Console.WriteLine(myIni.ReadString("DbServer", "DbFile", ""));
C# 配置文件ini操作类的更多相关文章
- 我也分享一个c# ini操作类
刚刚看了一篇 @云菲菲 的关于基于正则的INI辅助类文章:http://www.cnblogs.com/yunfeifei/p/4081977.html,作者写的不错.还看到评论处有一个的地址:htt ...
- Ini操作类
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...
- android操作ini工具类
package com.smarteye.common; import java.io.BufferedReader; import java.io.BufferedWriter; import ja ...
- Python 之configparser读取配置操作类
一.为什么要封装 我们为什么要封装,我相信你们在项目开发过程中深有体会,那么这个读取配置工具类,又是为了什么? 为了项目参数配置的灵活性,不要改动到源码 为了信息的安全(一定层面的),体现代码重用性 ...
- C# ini配置文件操作类
/// <summary> /// INI文件操作类 /// </summary> public class IniFileHelper { /// <summary&g ...
- python 提供INI配置文件的操作 ConfigParser
原文地址:http://www.cnblogs.com/pumaboyd/archive/2008/08/11/1265416.html 红色的为标注信息 +++++++++++++++++引用+++ ...
- Ini文件操作类
/// <summary> /// Ini文件操作类 /// </summary> public class Ini { // 声明INI文件的写操作函数 WritePriva ...
- C# 配置文件操作类
注意添加引用:System.Configuration: using System; using System.Collections.Generic; using System.Text; usin ...
- C# 文件操作类大全
C# 文件操作类大全 时间:2015-01-31 16:04:20 阅读:1724 评论:0 收藏:0 [点我收藏+] 标签: 1.创建文件夹 //usin ...
随机推荐
- mvc使用mongodb时objectId序列化与反序列化
前面有写使用自己的mvc 序列化工具即jsonNetResult.我这里结合之前写的jsonNetResult来做一个Json序列化工具,而且序列化ObjectId成一个字符串.详细代码例如以下 us ...
- HDU 3008 DP
基础DP题 打BOSS BOSS和自己都有100点血.玩家先手 每回合能够选择施放技能攻击(耗蓝,共n种)或者普通攻击(不耗蓝,伤害为1),BOSS每回合会攻击自己q点血,每回合自己会恢复t点法力 ...
- 移动匿名支付购物方案 A Lightweight Anonymous Mobile Shopping Scheme Based on DAA for Trusted Mobile Platform
- Python中range和xrange的异同之处
range 函数说明:range([start,] stop[, step]).依据start与stop指定的范围以及step设定的步长,生成一个序列. range演示样例: >> ...
- jquery 推断checkbox 是否选中
这是一个蛋疼的节奏.曾经写的代码如今失效了. jquery 推断checkbox 是否被选中,刚開始我是这样写的,并且没问题 $("#ziduana").attr("ch ...
- poj 2104 K-th Number(主席树,详细有用)
poj 2104 K-th Number(主席树) 主席树就是持久化的线段树,添加的时候,每更新了一个节点的线段树都被保存下来了. 查询区间[L,R]操作的时候,只需要用第R棵树减去第L-1棵树就是区 ...
- geckofx
geckofx是skybound工作室开发的一个开源的用于方便将gecko引擎(最主要的浏览器是firefox)链接到·net 窗体应用的一个组建. 外文名 geckofx 开发商 skyboun ...
- (多项式)因式分解定理(Factor theorem)与多项式剩余定理(Polynomial remainder theorem)(多项式长除法)
(多项式的)因式分解定理(factor theorem)是多项式剩余定理的特殊情况,也就是余项为 0 的情形. 0. 多项式长除法(Polynomial long division) Polynomi ...
- bzoj3663
几何+lis 很巧妙.直接做很困难,那么我们转化一下,把每个点能看见的圆弧画出来.只有这些圆弧相交时才满足条件. 那么也就是找出圆上尽量多两两相交的区间. 所以我们先按左端点极角排序,然后固定一个必须 ...
- smarty用法
smarty学习指南 在smarty的模板设计部分我简单的把smarty在模板中的一些常用设置做了简单的介绍,这一节主要来介绍一下如何在smarty中开始我们程序设计.下载Smarty文件放到你们站点 ...