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配置的更多相关文章

  1. Python使用ConfigParser模块读取配置文件(config.ini)以及写入配置文件

    前言 使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是configParser.configPars ...

  2. 用python读取配置文件config.ini

    还在学习中...写的有点凌乱 感觉还是应该先学会读取配置文件才行,把一些经常需要修改的但是又经常需要用到的参数放到配置文件中方便使用(我是这么觉得的) 首先是config.ini的存放位置,我们把它放 ...

  3. VS中C#读取app.config数据库配置字符串的三种方法(转)

    关于VS2008或VS2005中数据库配置字符串的三种取法 VS2008建立Form程序时,如果添加数据源会在配置文件 app.config中自动写入连接字符串,这个字符串将会在你利用DataSet, ...

  4. [转]WinForm和WebForm下读取app.config web.config 中邮件配置的方法

    本文转自:http://blog.csdn.net/jinbinhan/article/details/1598386 1. 在WinForm下读取 App.config中的邮件配置语句如下: Con ...

  5. 读取、设置 php.ini配置文件(复制)

    1.ini_get()获取配置参数,ini_set()设置配置参数 复制代码 代码如下: <?phpecho ini_get('display_errors'); //1//动态修改php.in ...

  6. MVC.Net:读取Web.config/App.config配置

    需要读取Web.config/App.config的配置很简单,首先我们需要将配置写入到<appSettings>中,例如: <appSettings> <add key ...

  7. golang 读取 ini配置信息

      package main //BY: 29295842@qq.com//这个有一定问题   如果配置信息里有中文就不行//[Server] ;MYSQL配置//Server=localhost   ...

  8. C#对config.ini文件进行读取和修改

    C#对config.ini文件进行读取和修改: public partial class Patrolcar : Form之后可以加入如下类: #region public class IniFile ...

  9. boost::property_tree 读取ini配置

    应用场景: 在后端服务器项目开发中,需要初始化一个Socket服务器,需要IP地址与对应端口号等参数:另外还可能因为对接数据库,就还需要数据库的相关配置参数,如我使用的是MySql数据库,就需要数据库 ...

随机推荐

  1. Python抓取糗事百科成人版图片

    最近开始学习爬虫,一开始看的是静觅的爬虫系列文章,今天看到糗事百科成人版,心里就邪恶了一下,把图片都爬下来吧,哈哈~ 虽然后来实现了,但还是存在一些问题,暂且不提,先切入正题吧,没什么好说的,直接上代 ...

  2. Python批量修改图片格式和尺寸

    Python批量修改图片格式和尺寸 备注: 1.导入了PIL库,是处理图片用的,很强大; 2.导入了的win32库,是判断隐藏文件用的,我们的项目需要删除隐藏文件,不需要的可以直接找到删除. 3.导入 ...

  3. 5G信令(就是用户身份信息)风暴——就是客户端通过公钥加密的消息(携带手机IMSI号)发给服务端,服务器需用私钥解密,这个解密比较消耗资源,如果短时间大量请求到来就会触发信令风暴

    信令:手机开机后,先从USIM中读取之前运营商分配的临时身份信息GUTI/TMSI,发送携带该身份信息的信令给基站,请求接入运营商网络. 如果每个设备的每条消息都需要单独认证,则网络侧安全信令的验证需 ...

  4. yyyy-MM-dd EEE hh:mm:ss(日期转换)

    <script> /** * 对Date的扩展,将 Date 转化为指定格式的String * 月(M).日(d).12小时(h).24小时(H).分(m).秒(s).周(E).季度(q) ...

  5. CF991C

    题解: 很显然不会有那么多种肯能 所以都列出来即可 代码: #include<bits/stdc++.h> using namespace std; int main() { ]; sca ...

  6. nodejs之log4js日志记录模块简单配置使用

    在我的一个node express项目中,使用了log4js来生成日志并且保存到文件里,生成的文件如下: 文件名字叫:access.log 如果在配置log4js的时候允许了同时存在多个备份log文件 ...

  7. py2exe生成.exe(python3.4 尝试)

    第一次成功将python3.4脚本生成 exe文件. 测试环境:win8.1 32位,python3.4,pyside py打包成exe的工具我所知道的有三种 cx-freeze , py2exe , ...

  8. redis安装配置记录

    环境:CentOS7,最小化安装 安装gcc wget # yum upgrade # yum install gcc # yum install wget 下载并安装redis # wget htt ...

  9. NI FPGA板卡程序设计概述

    NI公司提到了三种不同应用开发环境ADE:http://www.ni.com/white-paper/5956/zhs/ LabVIEW是NI公司主推的ADE,采用G语言(图像化语言),支持力度最大 ...

  10. a的样式

    .myAucCItem a { color: rgb(71,71,71);} .myAucCItem a:hover { color: rgb(71,71,71); text-decoration: ...