[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 ...
随机推荐
- TCP BBR - 一键安装最新内核并开启 TCP BBR
原文地址: https://teddysun.com/489.html 最近,Google 开源了其 TCP BBR 拥塞控制算法,并提交到了 Linux 内核,从 4.9 开始,Linux 内核已经 ...
- 【OCP 062新题】OCP题库更新出现大量新题-9
9.You ran this command on a source database: $> expdp hr/hr DIRECTORY=dumpdir DUMPFILE=empl.dmp V ...
- 【文文殿下】ExBSGS
无需逆元版本: #include<cstdio> #include<cassert> #include<cmath> #include<map> typ ...
- PHP 单点登录实现方案
单点登录SSO(Single Sign On)说得简单点就是在一个多系统共存的环境下,用户在一处登录后,就不用在其他系统中登录,也就是用户的一次登录能得到其他所有系统的信任.单点登录在大型网站里使用得 ...
- 看linux正在运行的服务用哪个命令?
https://zhidao.baidu.com/question/117779006.html 查看服务进程:ps aux查看服务cpu利用:top查看服务对应端口:netstat -nlp pst ...
- 用switch函数根据选择不同的radio出现不同的视图
html代码: <!DOCTYPE html> <html> <head> <title></title> <style type=& ...
- 腾讯云 利用php + apache + mysql 搭建服务器环境
1.一键安装需要的软件源 yum install -y httpd php php-fpm mysql mysql-server php-mysql 1) httpd 即为 apache 2)php ...
- Swift 学习指引
以下指引是基于最新的 Swift 4.0 为基础为而言. 如你在参考3.0以下版本, 那你就不要说你会 Swift, 3.0 之前是 Objective-C 的搬迁(80%),是不成熟的语言, 看着很 ...
- POJ 2304
#include<iostream>// cheng da cai zi 11. 18 解锁问题 using namespace std; #define f 360 int main() ...
- rabbitmq系列二 之工作队列
---恢复内容开始--- 1.工作队列的简介 在上一篇中,我们已经写了一个从已知队列中发送和获取消息的程序,在这里,我们创建一个工作队列(work queue), 会发送一些耗时的任务给多个工作者.模 ...