ini配置文件的读取
LANG]
language=zh-Hans
[LOGIN]
serverIP=你需要的服务器IP地址
userName=chaoyue
userPass=
[MAPINFO]
web1lat=34.2638
web1lng=108.9469
web2lat=34.2638
web2lng=108.9469
web1zoom=
web2zoom=
web1southWestlat=10.055402736564236
web1northEastlat=53.067626642387374
web1southWestlng=58.798828125
web1northEastlng=159.08203125
web2southWestlat=23.32208001137843
web2northEastlat=43.96119063892024
web2southWestlng=81.298828125
web2northEastlng=136.669921875
节
[section]
参数(键=值)
name=value
注解
注解使用分号表示(;)。在分号后面的文字,直到该行结尾都全部为注解。
; comment textINI文件的数据格式的例子(配置文件的内容) [Section1 Name]
KeyName1=value1
KeyName2=value2
...
[Section2 Name]
KeyName21=value21
KeyName22=value22
其中:
[Section1 Name]用来表示一个段落。
因为INI文件可能是项目中共用的,所以使用[Section Name]段名来区分不同用途的参数区。例如:[Section1 Name]表示传感器灵敏度参数区;[Section2 Name]表示测量通道参数区等等。
KeyName1=value1 用来表示一个参数名和值。
比如:
7033=50
7034=51
其中:
7033表示某传感器名,50表示它的灵敏度值。
7034表示另一只传感器名,51表示它的灵敏度值。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
namespace 你的命名空间
{
class CIni
{
//声明读写INI文件的API函数
public string path;
public CIni()
{
}
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
/// <summary>
/// ini文件编写器
/// </summary>
/// <param name="section">查找ini文件的节点[]名</param>
/// <param name="key">节点下边的键</param>
/// <param name="val">节点的值</param>
/// <param name="filePath">来自的文件</param>
public void WriterINI(string section, string key, string val,string filePath)
{
path = System.Environment.CurrentDirectory + "\\" + filePath;
WritePrivateProfileString(section, key, val, path);
}
/// <summary>
/// 读取Ini文件
/// </summary>
/// <param name="section">获得节点</param>
/// <param name="key">节点下边的键</param>
/// <param name="filePath">文件路径</param>
/// <returns>返回的值</returns>
public string ReadINI(string section, string key, string filePath)
{
path = System.Environment.CurrentDirectory + "\\" + filePath;
StringBuilder temp = new StringBuilder();
GetPrivateProfileString(section, key, "", temp, , path);
return temp.ToString().Trim();
}
}
}
用法:
/// <summary>
/// 读取Ini文件
/// </summary>
/// <param name="strConet">字段</param>
/// <param name="order"></param>
private void WriterINI(string strConet, int order)
{
GetNextToken(ref strConet, ",");
CIni ini = new CIni();
if (order == )
{
ini.WriterINI("MAPINFO", "web1lat", GetNextToken(ref strConet, ","), "Setting.ini");
ini.WriterINI("MAPINFO", "web1lng", GetNextToken(ref strConet, ","), "Setting.ini");
ini.WriterINI("MAPINFO", "web1zoom", GetNextToken(ref strConet, ","), "Setting.ini");
ini.WriterINI("MAPINFO", "web1southWestlat", GetNextToken(ref strConet, ","), "Setting.ini");
ini.WriterINI("MAPINFO", "web1northEastlat", GetNextToken(ref strConet, ","), "Setting.ini");
ini.WriterINI("MAPINFO", "web1southWestlng", GetNextToken(ref strConet, ","), "Setting.ini");
ini.WriterINI("MAPINFO", "web1northEastlng", GetNextToken(ref strConet, ","), "Setting.ini");
}
else
{
ini.WriterINI("MAPINFO", "web2lat", GetNextToken(ref strConet, ","), "Setting.ini");
ini.WriterINI("MAPINFO", "web2lng", GetNextToken(ref strConet, ","), "Setting.ini");
ini.WriterINI("MAPINFO", "web2zoom", GetNextToken(ref strConet, ","), "Setting.ini");
ini.WriterINI("MAPINFO", "web2southWestlat", GetNextToken(ref strConet, ","), "Setting.ini");
ini.WriterINI("MAPINFO", "web2northEastlat", GetNextToken(ref strConet, ","), "Setting.ini");
ini.WriterINI("MAPINFO", "web2southWestlng", GetNextToken(ref strConet, ","), "Setting.ini");
ini.WriterINI("MAPINFO", "web2northEastlng", GetNextToken(ref strConet, ","), "Setting.ini");
}
}
////GetNextToken方法
private string GetNextToken(ref string strContent, string strDelim)
{
string str = "";
int idx = strContent.IndexOf(strDelim, , strContent.Length);
if (idx > )
{
str = strContent.Substring(, idx);
strContent = strContent.Remove(, idx + );
}
else
{
str = strContent;
}
return str;
}
/// <summary>
/// 设置ini文件
/// </summary>
private void SetCenter()
{
CIni ini = new CIni();
string strweb1lat = ini.ReadINI("MAPINFO", "web1lat", "Setting.ini");
string strweb1lng = ini.ReadINI("MAPINFO", "web1lng", "Setting.ini");
string strweb1zoom = ini.ReadINI("MAPINFO", "web1zoom", "Setting.ini");
string strweb2lat = ini.ReadINI("MAPINFO", "web2lat", "Setting.ini");
string strweb2lng = ini.ReadINI("MAPINFO", "web2lng", "Setting.ini");
string strweb2zoom = ini.ReadINI("MAPINFO", "web2zoom", "Setting.ini");
//你的方法写在这里.....
}
ini配置文件的读取的更多相关文章
- 读取ini配置文件 及 UI对象库
读取ini配置文件 配置项 读取API 写入API 实战:UI 对象库 读取ini配置文件 配置项 在每个 ini 配置文件中,配置数据会被分组(比如下述配置文件中的"config" ...
- 用java读写ini配置文件
本文转载地址: http://www.blogjava.net/silvernapoleon/archive/2006/08/07/62222.html import java.io.Bu ...
- java 读写ini配置文件
ini配置文件 ;客户端配置[Client];客户端版本号version=0001;设备号devNum=6405 public final class ConfigurationFile { /** ...
- python读取uti-8格式ini配置文件出现UnicodeDecodeError: 'gbk' codec can't decode byte 0xba in position 367: illegal multibyte sequence错误解决方法
出现这种错误只需要在read下添加encoding='utf-8' 如: from configparser import ConfigParser cf = ConfigParser() cf.re ...
- Python读取ini配置文件的方式
python configparser模块 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...
- bat 读取 ini 配置文件
bat 读取 ini 配置文件 config.ini: abc=abc a=a localpath=D:\local\path .bat: @echo off setlocal enabledelay ...
- 转 python3 读取 ini配置文件
在代码中经常会通过ini文件来配置一些常修改的配置.下面通过一个实例来看下如何写入.读取ini配置文件. 需要的配置文件是: 1 [path] 2 back_dir = /Users/abc/Pych ...
- python读取ini配置文件的示例代码(仅供参考)
这篇文章主要介绍了python读取ini配置文件过程示范,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 安装 pip install configp ...
- python3笔记-读取ini配置文件
在代码中经常会通过ini文件来配置一些常修改的配置.下面通过一个实例来看下如何写入.读取ini配置文件. 需要的配置文件是: [path] back_dir = /Users/abc/PycharmP ...
随机推荐
- 处理form表单提交后返回值的处理办法【html5】
同事朋友ajax,最近在弄公司业务电话机,自主搭建,买的设备. 其中最主要功能是前端发起呼叫,通过浏览器触发设备进行呼叫功能,走后台呼叫还不行. 需求是这样的: 前端给设备ip发送特定的一段xml信息 ...
- Ubuntu 18.04安装 Sublime
作为一款强大的源代码编辑器,当然需要一手流利的命令来安装它. 命令: wget -qO - https://download.sublimetext.com/sublimehq-pub.gpg | s ...
- Linux下使进程在后台运行
怎么样使程序在后台执行 /////////////////// nohup ./nn > nn.log 2 > &1 & //////////// 方法有很多, ...
- Ionic-wechat项目边开发边学(一):环境搭建和创建一个项目
之前学AngularJS,教程过了一遍觉得很简单,但真正写几个Demo就错误百出,一个小小的功能要折腾很久.所以这次学Ionic,准备以开发一个项目为切入点去学,那么问题来了,开发什么项目呢? 纠结了 ...
- vue-music 关于Search(搜索页面)-- 搜索结果优化
搜索结果 列表点击跳转到相应的歌手详情页或者 歌曲页面,通过子路由跳转,和singer 组件一样 在suggest.vue 组件判断如果点击的是歌手,则new 一个歌手对象,通过这个对象的id 属性值 ...
- android:sharedUserId
<manifest> syntax: <manifest xmlns:android="http://schemas.android.com/apk/res/android ...
- 第八届山东省ACM大学生程序设计竞赛个人总结
因为省赛,从开学紧张到5月7号.心思也几乎全放在ACM的训练上.因为我还是校台球协会的会长,所以台协还有一些事情需要忙,但是我都给延迟了.老会长一直在催我办校赛,但我一直说 等等吧,因为校赛只能在周六 ...
- python3下安装aiohttp遇到过的那些坑
python3下安装aiohttp遇到过的那些坑 最近需要用到aiohttp这个库,在安装过程中遇到很多坑.google.baidu后,依然没有找到合适的解决方案. 后来通过去python官方的PyP ...
- Redux 中间件的执行顺序理解
Redux.applyMiddleware(thunk, middleware1) 和 Redux.applyMiddleware(middleware1, thunk) 的区别: <!DOCT ...
- EasyUI学习总结(五)——EasyUI组件使用(转载)
本文转载自:http://www.cnblogs.com/xdp-gacl/p/4084520.html 一.EasyUI组件的简单介绍 easyUI提供了很多组件让我们使用,如下图所示: