读取设置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数据库,就需要数据库 ...
随机推荐
- JAVA执行带参数的SQL语句
转自 http://www.cnblogs.com/raymond19840709/archive/2008/05/12/1192948.html
- iOS-不用微信SDK唤起微信支付
作者:TianBai 原文链接:http://www.jianshu.com/p/8930b4496023 要想知道微信SDK是如何调起微信客户端,那么咱们先看看微信SDK到底做了什么 前期准备 接入 ...
- MySQL使用通用二进制格式安装
CentOS7安装MySQL的方法之通用二进制格式
- expdp/impdp 详细参数解释
任意可以使用expdp/impdp的环境,都可以通过help=y看到帮助文档. 1.expdp参数说明 2.impdp参数说明 3.expdp参数说明(中文) 4.impdp参数说明(中文) expd ...
- eclipse背景设置什么颜色缓解眼睛疲劳之一
Eclipse操作界面默认颜色为白色.对于我们长期使用电脑编程的人来说,白色很刺激我们的眼睛,如果把颜色改成绿色的颜色就会缓解眼睛的疲劳. 设置方法如下: 1.打开window->Prefere ...
- iOS 可能用到的三方框架
1.MWPhotoBrowser 第三方图片浏览器 https://github.com/mwaterfall/MWPhotoBrowser 2.SlackTextViewController 强大 ...
- C++面向对象高级编程(八)模板
技术在于交流.沟通,转载请注明出处并保持作品的完整性. 这节课主要讲模板的使用,之前我们谈到过函数模板与类模板 (C++面向对象高级编程(四)基础篇)这里不再说明 1.成员模板 成员模板:参数为tem ...
- JVM的理解
1.Java程序是交由JVM执行的,所以我们在谈Java内存区域划分的时候事实上是指JVM内存区域划分.在讨论JVM内存区域划分之前,先来看一下Java程序具体执行的过程: 也相当与 注:JVM(ja ...
- 1. Java EE简介 - JavaEE基础系列
什么是Java EE? 真的是你理解的那样吗? Java EE, 原名J2EE, 其核心由一系列抽象的标准规范所组成, 是针对目前软件开发中所普遍面临问题的解决方案. 注意以上定义中的"抽象 ...
- Tensorflow 运行警告提示 Your CPU supports instructions that this TensorFlow binary was not compiled to use
由于现在神经网络这个东西比较火,准确的说是深度学习这个东西比较火,我们实验室准备靠这个东西发几个CCF A类的文章,虽然我不太懂这东西,兴趣也一般都是毕竟要跟随主流的,于是今天安装起了 Tensorf ...