C#- 操作Ini文件
以前习惯了使用.NET中的WEB.CONFIG或者APP.CONFIG,最近在做项目的时候遇到了些问题,发现没办法使用这些CONFIG文件。一开始我的做法是建一个文本文件,自己定规律,自己写方法去写新的配置项和读配置项,虽然基本都能实现了,但做的很一般,不够简洁明了。然后我想过用XML来做配置文件,最后又发现INI文件。INI文件已经有别人写好的DLL可以用了,了解多了一些后看到很多软件也用这INI,包括我们的WINDOWS系统也用了很多INI文件,我用了下感觉很不错。
在网上的了一些文章,再整理了一下下,记录如下,做一个备忘
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices; namespace IniFileDemo
{
public class IniFile
{
public string Path; public IniFile(string path)
{
this.Path = path;
} #region 声明读写INI文件的API函数
[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 defVal, StringBuilder retVal, int size, string filePath); [DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string defVal, Byte[] retVal, int size, string filePath);
#endregion /// <summary>
/// 写INI文件
/// </summary>
/// <param name="section">段落</param>
/// <param name="key">键</param>
/// <param name="iValue">值</param>
public void IniWriteValue(string section, string key, string iValue)
{
WritePrivateProfileString(section, key, iValue, this.Path);
} /// <summary>
/// 读取INI文件
/// </summary>
/// <param name="section">段落</param>
/// <param name="key">键</param>
/// <returns>返回的键值</returns>
public string IniReadValue(string section, string key)
{
StringBuilder temp = new StringBuilder(); int i = GetPrivateProfileString(section, key, "", temp, , this.Path);
return temp.ToString();
} /// <summary>
/// 读取INI文件
/// </summary>
/// <param name="Section">段,格式[]</param>
/// <param name="Key">键</param>
/// <returns>返回byte类型的section组或键值组</returns>
public byte[] IniReadValues(string section, string key)
{
byte[] temp = new byte[]; int i = GetPrivateProfileString(section, key, "", temp, , this.Path);
return temp;
} }
}
读写DEMO
private void button1_Click(object sender, EventArgs e)
{
IniFile ini = new IniFile(@"d:\abc.ini");
//读
//byte[] sectionByte = ini.IniReadValues("Person", "Love");
//string result = System.Text.Encoding.ASCII.GetString(sectionByte);
//MessageBox.Show(result);
//写
//ini.IniWriteValue("Person", "Love", "Girl");
}
C#- 操作Ini文件的更多相关文章
- 关于C#操作INI文件的总结
原文:关于C#操作INI文件的总结 INI文件其实是一种具有特定结构的文本文件,它的构成分为三部分,结构如下: [Section1]key 1 = value2key 1 = value2--[S ...
- C#利用Vini.cs操作INI文件
VClassLib-CS项目Github地址:https://github.com/velscode/VClassLib-CS VINI文档地址:https://github.com/velscode ...
- [转]C#操作INI文件
在很多的程序中,我们都会看到有以.ini为后缀名的文件,这个文件可以很方便的对程序配置的一些信息进行设置和读取,比如说我们在做一个程序后台登陆的时候,需要自动登录或者是远程配置数据库连接,及保存密码设 ...
- QSettings配置读写-win注册表操作-ini文件读写
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QSettings配置读写-win注册表操作-ini文件读写 本文地址:http:// ...
- C#操作INI文件(明天陪你看海)
C#操作INI文件 在很多的程序中,我们都会看到有以.ini为后缀名的文件,这个文件可以很方便的对程序配置的一些信息进行设置和读取,比如说我们在做一个程序后台登陆的时候,需要自动登录或者是远程配置数据 ...
- 【转】操作ini文件
一.INI文件的结构: ; 注释 [小节名] 关键字=值 INI文件有多个小节,每个小节又有多个关键字, “=”后面是该关键字的值. 值的类型有三种:字符串.整型数值和布尔值. 其中字符串存贮在IN ...
- .net操作InI文件
public class INI { public static string IniFileName = "";//路径 [DllImport("kernel32&qu ...
- Delphi操作Ini文件
Delphi提供了一个TInifile类,使我们可以非常灵活的处理INI文件 一.INI文件的结构[小节名]ini文件 关键字1=值1 关键子2=值2INI文件允许有多个小节, ...
- .net 操作INI文件
using System.Runtime.InteropServices; using System.Text; namespace FaureciaManager { public class Fi ...
- python操作ini文件
简介 ini文件作为常见的配置文件,因此需要对ini文件做处理,此处使用configparser模块,本文介绍以下ini文件常用的处理方式. 需要读取的ini文件 如下文件,[ ]包含的称为secti ...
随机推荐
- php分页类的二种调用方法(转载)
php分页类的二种调用方法 原文地址:http://www.xfcodes.com/php/fenye/25584.htm 导读:php分页类的二种调用用法,ajax调用php分页类,非ajax方式调 ...
- 背投广告js
js: $('#appwrap').append('<a href="http://web.2144.cn/gateway/reg?pos=8966" id="ad ...
- 用 Maven 做项目构建
转自:http://www.ibm.com/developerworks/cn/java/j-lo-maven/index.html 本文将介绍基于 Apache Maven 3 的项目构建的基本概念 ...
- 1013: [JSOI2008]球形空间产生器sphere
很直观的一个gauss题: 用的是以前用过的一个模板: #include<cstdio> #include<algorithm> #include<cmath> # ...
- C++引用的实质
转自探索c++的底层机制 在看这篇文章之前,请你先要明白一点:那就是c++为我们所提供的各种存取控制仅仅是在编译阶段给我们的限制,也就是说是编译器确保了你在完成任务之前的正确行为,如果你的行为不正确, ...
- 李洪强漫谈iOS开发[C语言-024]-表达式与赋值运算符
- 安装Ubuntu服务器
安装edX首先需要一台linux或Mac系统的电脑/服务器. 这里以常见的Ubuntu作为服务器系统. Ubuntu的官方网站为http://www.ubuntu.com,中文网站为http://ht ...
- 【HDOJ】1263 水果
hash,使用stl map ac.学了find_if等强大的东西,第一次使用stl模板. #include <iostream> #include <cstdio> #inc ...
- 更新你的jar包
jar文件:/home/resin.jar需更新包中com/caucho/server/port/Port.class类文件 方法1:jar uf resin.jar com/caucho/serve ...
- scaleform mobile sdk for android 多点触摸 修正
修正 scaleform 的多点触控 (随手一记 给后来的人做个参考) scaleform 版本号 4.2.24 (估计这就是最后一个 移动版的版本了,万年没有更新了) 开始 一直以为 scalefo ...