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内置函数详解-总结篇

    参考链接:http://www.cnblogs.com/sesshoumaru/p/6140987.html

  2. vue-router防跳墙控制

    vue-router防跳墙控制 因为在实际开发中,从自己的角度来看,发现可以通过地址栏输入地址,便可以进入本没有权限的网页.而我们一般只是操作登录页面,其他页面很少考虑,此刻特来尝试解决一下 基于vu ...

  3. 使用IScroll5实现滚动

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 004-对象——public protected private PHP封装的实例

    <?php /** *public protected private PHP封装的实例 */ /*class tv { private $shengyin; function __constr ...

  5. RMAN中format的参数

    (转自:http://blog.chinaunix.net/uid-23079711-id-2554290.html) format 的替换变量,注意大小写! 1.     %d --数据库的db_n ...

  6. java程序设计基础篇 复习笔记 第七单元&&第八单元

    7.1 int[][] triArray{ {1}, {1,2}, {1,2,3}, }; 7.2 array[2].length 8.1 Unified Modeling Language:UML ...

  7. 十五、dbms_space(分析段增长和空间的需求)

    1.概述 作用:用于分析段增长和空间的需求. 2.包的组成 1).unused_space作用:用于返回对象(表.索引.簇)的未用空间语法:dbms_space.unused_space(segmen ...

  8. eureka-5- Eureka 的自我保护模式

    当我们登录Eureka Dashboard ,如果看到首页显示如下,则表示,Eureka进入自我保护模式. 默认情况下,Eureka Server 在一定时间内没有接受到某个服务实例的心跳,Eurek ...

  9. Python基础学习----列表

      name_list=["张无忌","张三丰","张小明","胡歌","夏东海"] #循环输出na ...

  10. java读取resource/通过文件名获取文件类型

    java读取resource java读取resource目录下文件的方法: 借助Guava库的Resource类 Resources.getResource("test.txt" ...