// [ 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操作类的更多相关文章

  1. 我也分享一个c# ini操作类

    刚刚看了一篇 @云菲菲 的关于基于正则的INI辅助类文章:http://www.cnblogs.com/yunfeifei/p/4081977.html,作者写的不错.还看到评论处有一个的地址:htt ...

  2. Ini操作类

    using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...

  3. android操作ini工具类

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

  4. Python 之configparser读取配置操作类

    一.为什么要封装 我们为什么要封装,我相信你们在项目开发过程中深有体会,那么这个读取配置工具类,又是为了什么? 为了项目参数配置的灵活性,不要改动到源码 为了信息的安全(一定层面的),体现代码重用性 ...

  5. C# ini配置文件操作类

    /// <summary> /// INI文件操作类 /// </summary> public class IniFileHelper { /// <summary&g ...

  6. python 提供INI配置文件的操作 ConfigParser

    原文地址:http://www.cnblogs.com/pumaboyd/archive/2008/08/11/1265416.html 红色的为标注信息 +++++++++++++++++引用+++ ...

  7. Ini文件操作类

    /// <summary> /// Ini文件操作类 /// </summary> public class Ini { // 声明INI文件的写操作函数 WritePriva ...

  8. C# 配置文件操作类

    注意添加引用:System.Configuration: using System; using System.Collections.Generic; using System.Text; usin ...

  9. C# 文件操作类大全

      C# 文件操作类大全 时间:2015-01-31 16:04:20      阅读:1724      评论:0      收藏:0      [点我收藏+] 标签: 1.创建文件夹 //usin ...

随机推荐

  1. zoj——3557 How Many Sets II

    How Many Sets II Time Limit: 2 Seconds      Memory Limit: 65536 KB Given a set S = {1, 2, ..., n}, n ...

  2. Codeforces 300E(数学)

    题意:给定k个数字,求最小的正整数n,使得“n的阶乘”是“这k个数字的阶乘的积”的倍数.1<=k<=1e6,数字ai满足1<=ai<=1e7 分析:如果我们能对着k个数字的阶乘 ...

  3. Ubuntu 16.04安装设备管理器Hardinfo和lshw设备信息命令

    安装: sudo apt-get install hardinfo 启动: 实际上这些信息都可以通过lshw进行查看,参考:https://linux.die.net/man/1/lshw

  4. 晶振虚焊导致TI 28335 DSP 烧写FLASH后,连接仿真器时正常工作,拔掉仿真器却不能启动运行

    遇到个诡异的问题,28335的DSP,之前程序调试一切正常,但是烧写FLASH后,拔掉仿真器却始终部工作. 解决思路: 1) 检查配置文件貌似没什么问题,复制到其他工程,在开发板上拔掉仿真器启动正常. ...

  5. JFrame实现批量获取Android安装包安全证书MD5

    今天遇到一个需求.获取全部apk的签名的MD5.以下是我使用Java SE实现的一个工具.贴出核心源码.希望给有须要的朋友有所帮助. 界面例如以下: 仅仅须要制定.apk文件所在的文件夹就可以,核心代 ...

  6. linux高级技巧:集群之keepalived

    1.keepalived简单介绍         Keepalived是一个基于VRRP协议来实现的WEB服务高可用方案.能够利用其来避免单点故障.使用多台节点安装keepalived. 其它的节点用 ...

  7. DB9针型:RS485输出信号及接线端子引脚分配

    下图所看到的.DB9针型RS485输出信号及接线端子引脚分配. 此DB9针型与 标准 RS232 or RS485 DB9定义有所不同,下图中的DB9针型说明仅是针对USB转485DB9接口. wat ...

  8. 技术架构model

  9. go语言笔记——go是有虚拟机runtime的,不然谁来做GC呢,总不会让用户自己来new和delete进行内存管理吧,还有反射!Go 的 runtime 嵌入到了每一个可执行文件当中

    2.7 Go 运行时(runtime) 尽管 Go 编译器产生的是本地可执行代码,这些代码仍旧运行在 Go 的 runtime(这部分的代码可以在 runtime 包中找到)当中.这个 runtime ...

  10. 【POJ 2259】 Team Queue

    [题目链接] http://poj.org/problem?id=2259 [算法] 由题,一个人入队时,若这个人所在的组已经有人在队列中,则加入队列,否则排到队末 因此我们发现,这个队列一定是由连续 ...