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. SPOJ BALNUM ★(位压缩状态+数位DP)

    题意 求区间[A,B]上的平衡数个数.平衡数是这样的数:在数的各个位上,奇数数字出现偶数次,偶数数字出现奇数次. 思路 很明显我们需要记录每一位出现的次数.分别记录是不明智的,而我们又只需要记录奇数次 ...

  2. Spring源码解析-IOC容器的实现-ApplicationContext

    上面我们已经知道了IOC的建立的基本步骤了,我们就可以用编码的方式和IOC容器进行建立过程了.其实Spring已经为我们提供了很多实现,想必上面的简单扩展,如XMLBeanFacroty等.我们一般是 ...

  3. node csrf 防御 待续

    csrf 防御 token 与 ajax 主要是在cookie添加随机数, 因为攻击者 无法访问第三方网站的 cookie,  加上httponly, 即使是xss也无法访问了 也可以在页面上嵌入一个 ...

  4. 使用jQuery操作DOM(2)

    1.获取设置属性值 attr({属性名1:属性值1,属性名2:属性值2}); //设置属性值 removeAttr(“属性名”); //删除属性 注意:如果元素没有此属性,会不起作用 2.获取同辈元素 ...

  5. java高并发下的数据安全

    高并发下的数据安全 我们知道在多线程写入同一个文件的时候,会存现“线程安全”的问题(多个线程同时运行同一段代码,如果每次运行结果和单线程运行的结果是一样的,结果和预期相同,就是线程安全的).如果是My ...

  6. 《利用Python进行数据分析》笔记---第4章NumPy基础:数组和矢量计算

    写在前面的话: 实例中的所有数据都是在GitHub上下载的,打包下载即可. 地址是:http://github.com/pydata/pydata-book 还有一定要说明的: 我使用的是Python ...

  7. 跟着小程学微服务-Mock自动化系统的原理及实现

    一.前言 在之前的文章 http://blog.csdn.net/u013970991/article/details/54862772 中已经介绍了"自动化Mock系统0.9版本" ...

  8. Ethernet、VLAN、QinQ

    以太网帧格式: 各字段解释: DMAC:目的MAC地址,该字段确定帧的接收者. SMAC:源MAC地址,该字段标识发送帧的工作站. Type:上层协议类型(0x0800:IP;0x0808:ARP;0 ...

  9. swift 定义枚举和结构体 及使用

    //定义枚举 enum MapDirection { case North case South case East case West func simpleDescription() -> ...

  10. redis状态监控与性能调优

    本文主要介绍及演示一些Redis相关的状态监控和性能调优的命令及使用方法: 1.redis-benchmark redis基准信息,redis服务器性能检测 例如: 检测redis服务器性能,本机63 ...