[C#]INI文件控制类
INI文件常用于保存各类设置或本地化文本,大概格式如下:
[Section]
key=value
然而.NET框架似乎并没有提供一个实用的工具来操作它,或许是因为MS想让我们都使用Settings类控制的config文件?
但是出于多种原因,我还是不太喜欢用Settings类以及这个XML格式的config文件。
幸运的是,有两个Win32API可以帮我们完成INI文件的控制:
WritePrivateProfileString
GetPrivateProfileString
但是非常尴尬的是这俩一个能写入中文,另一个却读不好中文。。。
于是只好自己动手丰衣足食了,谨记于此以备日后又有所需:
abstract class ConfigurationBase
{
public abstract string Path { get; } public abstract string Section { get; } /// <summary>
/// 指定好编码格式就能支持各种语言文字了
/// </summary>
private readonly Encoding encoding = Encoding.UTF8; public void Clear()
{
File.Delete(Path);
} public void Save()
{
File.WriteAllLines(Path, lines.ToArray(), encoding);
} private readonly List<string> lines; protected ConfigurationBase()
{
if (File.Exists(Path))
{
lines = new List<string>(
File.ReadAllLines(Path, encoding));
}
else
{
lines = new List<string>();
}
} protected string Get(string key, string defaultVal)
{
if (lines.Count != )
{
string sectionLine = String.Format("[{0}]", Section);
string keyLine = String.Format("{0}=", key);
Regex otherSection = new Regex(@"^\[[^\]+]\]$", RegexOptions.Compiled); bool inSection = false;
foreach (string line in lines)
{
if (sectionLine == line)
{
inSection = true;
}
else if (otherSection.IsMatch(line))
{
if (inSection) break;
}
else if (inSection && line.StartsWith(keyLine))
{
return line.Substring(keyLine.Length);
}
}
}
return defaultVal;
} protected void Set(string key, string value)
{
string sectionLine = String.Format("[{0}]", Section);
string keyLine = String.Format("{0}=", key);
Regex otherSection = new Regex(@"^\[[^\]+]\]$", RegexOptions.Compiled);
string valueLine = String.Format("{0}{1}", keyLine, value); bool inSection = false;
for (int i = ; i < lines.Count; i++)
{
if (sectionLine == lines[i])
{
inSection = true;
}
else if (otherSection.IsMatch(lines[i]))
{
if (inSection)
{
lines.Insert(i, valueLine);
break;
}
}
else if (inSection && lines[i].StartsWith(keyLine))
{
lines[i] = valueLine;
}
}
if (inSection)
{
lines.Add(valueLine);
}
else
{
lines.Add(sectionLine);
lines.Add(valueLine);
}
}
}
[C#]INI文件控制类的更多相关文章
- android操作ini工具类
package com.smarteye.common; import java.io.BufferedReader; import java.io.BufferedWriter; import ja ...
- C# 配置文件ini操作类
// [ DllImport ( "kernel32" ) ] //private static extern long WritePrivateProfileString ( s ...
- 我也分享一个c# ini操作类
刚刚看了一篇 @云菲菲 的关于基于正则的INI辅助类文章:http://www.cnblogs.com/yunfeifei/p/4081977.html,作者写的不错.还看到评论处有一个的地址:htt ...
- C# 读取Ini配置文件类
配置文件 为fileName.ini 的文件 第一行必须为空,不然读不出值 [section1] key=value key2=value ......... [section2] key=value ...
- c#读取INI文件类
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;na ...
- 自己写的 读写 ini 配置文件类
/// <summary> /// 不调用系统API 读写 ini 配置文件 /// </summary> public class RW_ini { #region ==== ...
- Ini操作类
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...
- C# INI配置文件读写类
ini是一种很古老的配置文件,C#操作ini文件借助windows底层ini操作函数,使用起来很方便: public class IniHelper { [DllImport("kernel ...
- C# Ini配置文件
public class INIUserAccound { static IniFile Ini = new IniFile(AppDomain.CurrentDomain.BaseDirectory ...
随机推荐
- linux文件的硬连接和软连接
建立软连接:ln -s 原路径 目标路径 原理示意图: 特点: 1. 相当于win中的快捷方式 2. 删除链接文件,源文件不受影响 3. 删除源文件,链接文件失效 4. ...
- MiniUi中ComboBox与Autocomplete的区别
ComboBox 下拉选择框与Autocomplete的区别: 1,ComboBox只在页面加载时加载一次,发送一次请求: 2,Autocomplete会根据用户输入的值动态的去发送请求加载数据:
- 【NOIP2017】 宝藏 状压dp
为啥我去年这么菜啊..... 我现在想了$20min$后打了$10min$就过了$qwq$. 我们用$f[i][j]$表示当前深度为$i$,访问了状态$j$中的所有点的最小代价. 显然$f[i][j] ...
- Java中equals,hashcode
在Java语言中,Object对象中包含一个equals和hashCode方法,其中hashCode方法是由JVM本地代码(native code)实现的,返回值是一个有符号的32位整数,对 ...
- Scrapy源码注解--CookiesMiddleware
class CookiesMiddleware(object): """ 中间件在Scrapy启动时实例化.其中jars属性是一个默认值为CookieJar对象的dict ...
- [转]Elasticsearch Java API总汇
http://blog.csdn.net/changong28/article/details/38445805#comments 3.1 集群的连接 3.1.1 作为Elasticsearch节点 ...
- Mapreduce部署与第三方依赖包管理
Mapreduce部署是总会涉及到第三方包依赖问题,这些第三方包配置的方式不同,会对mapreduce的部署便捷性有一些影响,有时候还会导致脚本出错.本文介绍几种常用的配置方式: 1. HADOOP_ ...
- Dubbo-Centos7管控台安装
1.下载Tomcat7: $ wget http://mirrors.hust.edu.cn/apache/tomcat/tomcat-7/v7.0.57/bin/apache-tomcat-7.0. ...
- [转] 使用 MVC 5 的 EF6 Code First 入门 系列
译文:http://www.cnblogs.com/Bce-/category/573301.html 原文:http://www.asp.net/mvc/overview/getting-start ...
- Android 开发工具类 25_getJSON
获取 JSON 数据并解析 import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; im ...