读取设置config.ini配置
class CSenseIni
{
/************************************************************************/
/*写操作
* strSection 节
* strKey 键
* strValue 需要写入的值
* strFilePath 配置文件的全路径(wince中使用相对路径)
*/
/************************************************************************/
public static void WriteIni(string strSection, string strKey, string strValue, string strFilePath)
{
string strCurr = CPlatformConfig.CurrentPath;
if (strCurr.Length < )
strCurr += strFilePath;
else
strCurr += "\\" + strFilePath;
INICommon(false, strSection, strKey, strValue, strCurr);
} /************************************************************************/
/* 读操作
* strSection 节
* strKey 键
* strDefault 如果未找到相应键对应的值则填入此值
* strFilePath 配置文件的全路径(wince中只能使用绝对全路径)
* 返回: 指定键的相应值
* 说明: 如果在文件中未找到相应节则添加,未找到相应键亦添加,如果键对应的值为空串则使用默认值填充ini文件并返回
/************************************************************************/
public static string GetIni(string strSection, string strKey, string strDefault, string strFilePath)
{
string strCurr = CPlatformConfig.CurrentPath;
if (strCurr.Length < )
strCurr += strFilePath;
else
strCurr += "\\" + strFilePath;
return INICommon(true, strSection, strKey, strDefault, strCurr);
} private static string[] Split(string input, string pattern)
{
string[] arr = System.Text.RegularExpressions.Regex.Split(input, pattern);
return arr;
}
private static void AppendToFile(string strPath, string strContent)
{
if (strContent.Length == )
return; FileStream fs = new FileStream(strPath, FileMode.Append);
StreamWriter streamWriter = new StreamWriter(fs, System.Text.Encoding.Default);
streamWriter.BaseStream.Seek(, SeekOrigin.End);
streamWriter.WriteLine(strContent);
streamWriter.Flush();
streamWriter.Close();
fs.Close();
}
private static void WriteArray(string strPath, string[] strContent)
{
FileStream fs = new FileStream(strPath, FileMode.Truncate);
StreamWriter streamWriter = new StreamWriter(fs, System.Text.Encoding.Default);
streamWriter.BaseStream.Seek(, SeekOrigin.Begin); for (int i = ; i < strContent.Length; i++)
{
if (strContent[i].Trim() == "\r\n")
continue;
if (strContent[i].Trim() == "")
continue;
streamWriter.WriteLine(strContent[i].Trim());
} streamWriter.Flush();
streamWriter.Close();
fs.Close();
}
//INI解析
private static string INICommon(bool isRead, string ApplicationName, string KeyName, string Default, string FileName)
{
string strSection = "[" + ApplicationName + "]";
string strBuf;
try
{
//a.文件不存在则创建
if (!File.Exists(FileName))
{
FileStream sr = File.Create(FileName);
sr.Close();
}
//读取INI文件
System.IO.StreamReader stream = new System.IO.StreamReader(FileName, System.Text.Encoding.Default);
strBuf = stream.ReadToEnd();
stream.Close();
}
catch (Exception e)
{ return Default;
} string[] rows = Split(strBuf, "\r\n");
string oneRow;
int i = ;
for (; i < rows.Length; i++)
{
oneRow = rows[i].Trim(); //空行
if ( == oneRow.Length)
continue; //此行为注释
if (';' == oneRow[])
continue; //没找到
if (strSection != oneRow)
continue; //找到了
break;
} //b.没找到对应的section,创建一节并创建属性
if (i >= rows.Length)
{
AppendToFile(FileName, "\r\n" + strSection + "\r\n" + KeyName + "=" + Default);
return Default;
} //找到section i += ; //跳过section int bakIdxSection = i;//备份section的下一行 string[] strLeft; //查找属性
for (; i < rows.Length; i++)
{
oneRow = rows[i].Trim(); //空行
if ( == oneRow.Length)
continue; //此行为注释
if (';' == oneRow[])
continue; //越界
if ('[' == oneRow[])
break; strLeft = Split(oneRow, "="); if (strLeft == null || strLeft.Length != )
continue; //找到属性
if (strLeft[].Trim() == KeyName)
{
//读
if (isRead)
{
//c.找到属性但没有值
if ( == strLeft[].Trim().Length)
{
rows[i] = strLeft[].Trim() + "=" + Default;
WriteArray(FileName, rows);
return Default;
}
else
{
//找到了
return strLeft[].Trim();
}
} //写
else
{
rows[i] = strLeft[].Trim() + "=" + Default;
WriteArray(FileName, rows);
return Default;
}
}
} //d.没找到对应的属性,创建之并赋为默认
rows[bakIdxSection] = rows[bakIdxSection] + "\r\n" + KeyName + "=" + Default;
WriteArray(FileName, rows);
return Default;
}
}
/* 获取路径*/
public class CPlatformConfig
{
private static string m_CurrentPath; private static string Platform
{
get
{
return Environment.OSVersion.Platform.ToString();
}
} //获取执行文件所在文件夹的绝对位置
public static string CurrentPath
{
get
{
//m_CurrentPath = @"D:\Index";
m_CurrentPath = AppDomain.CurrentDomain.BaseDirectory;
//if (Platform.Equals("WinCE"))
//{
// m_CurrentPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
//}
//else if (Platform.Equals("Win32NT"))
//{
// m_CurrentPath = Directory.GetCurrentDirectory();
//} return m_CurrentPath;
}
set
{
m_CurrentPath = value;
}
}
}
public class CSenseConfig
{
//获取&设置 语言: 0-中文 1-英文
public static int GetLanguage()
{
string szLang = CSenseIni.GetIni("Language", "IsEnglish", "", "config.ini");
return Convert.ToInt32(szLang);
} public static void SetLanguage(int iLanguge)
{
CSenseIni.WriteIni("Language", "IsEnglish", iLanguge.ToString(), "config.ini");
} }
读取设置config.ini配置的更多相关文章
- Python使用ConfigParser模块读取配置文件(config.ini)以及写入配置文件
前言 使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是configParser.configPars ...
- 用python读取配置文件config.ini
还在学习中...写的有点凌乱 感觉还是应该先学会读取配置文件才行,把一些经常需要修改的但是又经常需要用到的参数放到配置文件中方便使用(我是这么觉得的) 首先是config.ini的存放位置,我们把它放 ...
- VS中C#读取app.config数据库配置字符串的三种方法(转)
关于VS2008或VS2005中数据库配置字符串的三种取法 VS2008建立Form程序时,如果添加数据源会在配置文件 app.config中自动写入连接字符串,这个字符串将会在你利用DataSet, ...
- [转]WinForm和WebForm下读取app.config web.config 中邮件配置的方法
本文转自:http://blog.csdn.net/jinbinhan/article/details/1598386 1. 在WinForm下读取 App.config中的邮件配置语句如下: Con ...
- 读取、设置 php.ini配置文件(复制)
1.ini_get()获取配置参数,ini_set()设置配置参数 复制代码 代码如下: <?phpecho ini_get('display_errors'); //1//动态修改php.in ...
- MVC.Net:读取Web.config/App.config配置
需要读取Web.config/App.config的配置很简单,首先我们需要将配置写入到<appSettings>中,例如: <appSettings> <add key ...
- golang 读取 ini配置信息
package main //BY: 29295842@qq.com//这个有一定问题 如果配置信息里有中文就不行//[Server] ;MYSQL配置//Server=localhost ...
- C#对config.ini文件进行读取和修改
C#对config.ini文件进行读取和修改: public partial class Patrolcar : Form之后可以加入如下类: #region public class IniFile ...
- boost::property_tree 读取ini配置
应用场景: 在后端服务器项目开发中,需要初始化一个Socket服务器,需要IP地址与对应端口号等参数:另外还可能因为对接数据库,就还需要数据库的相关配置参数,如我使用的是MySql数据库,就需要数据库 ...
随机推荐
- 十九 Python分布式爬虫打造搜索引擎Scrapy精讲—css选择器
css选择器 1. 2. 3. ::attr()获取元素属性,css选择器 ::text获取标签文本 举例: extract_first('')获取过滤后的数据,返回字符串,有一个默认参数,也就是如 ...
- Hibernate中"二级缓存"配置
实体类 : package cn.happy.entity; public class Emp { private Integer empNo; private String empName; pub ...
- SSH集成log4j日志环境
第一步:在web.xml初始化log4j <context-param> <param-name>contextConfigLocation</param-name> ...
- linux命令权限
linux-命令权限 1) 新建用户natasha,uid为1000,gid为555,备注信息为“master” 2) 修改natasha用户的家目录为/Natasha 3) 查看用户信息 ...
- 007PHP基础知识——类型转换 外部变量
<?php /**类型转换 */ /*1.自由转换*/ /*2.强制转换:不改变原变量,生成新的变量*/ //转换为字符串: /*$a=100; $b=(string)$a; var_dump( ...
- TCP/IP网路协议复习
1.OSI (Open System Interconnect Protocol) 开放互联协议,这是一个七层的计算机网络协议,包括:物理层.数据链路层.网络层.传输层.回话层.表示层.应用层. ...
- 基于IDEA和Maven的SSM分层项目搭建
前言 虽然现在SpringBoot开始流行,但是SSM作为一个经典框架,还是有必要去了解一下. 项目建立 1.新建一个空白的Maven项目,如下图.然后把IDEA自动生成的多余src目录删掉. 2.右 ...
- codis3.2安装配置中的一些问题
1.参考文档与参考资料问题 安装codis集群之前,我先在网上找资料,然后又到github的项目官方地址找,不得不说,相关的资料不好找,而且找到之后有些东西说的也不是很清楚.由于codis版本迭代的问 ...
- L157
UK Watchdog: Smugglers to Exploit Border if no Brexit DealSmugglers and other organized criminals ar ...
- c# 实体处理工具类
using System; using System.Collections; using System.Collections.Generic; using System.ComponentMode ...