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 ...
随机推荐
- 一款你不容错过的Laravel后台管理扩展包 —— Voyager
http://laravelacademy.org/post/6401.html Posted on 2016年11月1日 by 学院君 1.简介 Voyager是一个你不容错过的Laravel后 ...
- 转:GitHub 万星推荐成长技术清单
转:http://www.4hou.com/info/news/7061.html 最近两天,在reddit安全板块和Twitter上有个GitHub项目很火,叫“Awesome Hacking”. ...
- 推荐开源靶场Vulhub
转:https://github.com/phith0n/vulhub Vulhub - Some Docker-Compose files for vulnerabilities environme ...
- markdown与html之间转换引发的问题
https://www.hackersb.cn/hacker/235.html 看了这位师傅的文章有感而发 前言 对于支持markdown语法的网站,一般都是在后端将markdown语法渲染为html ...
- 160. Intersection of Two Linked Lists【Easy】【求两个单链表的第一个交点】
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- [hdu-3007]Buried memory 最小覆盖圆
大致题意: 平面上有n个点,求一个最小的圆覆盖住所有点 最小覆盖圆裸题 学习了一波最小覆盖圆算法 #include<cstdio> #include<iostream> #in ...
- python之面向对象编程二
类的成员 类的成员可以分为三大类:字段.方法.属性. 字段:普通字段.静态字段. 方法:普通方法.类方法.静态方法 属性:普通属性. 注:所有成员中,只有普通字段的内容保存对象中,即:根据此类创建了多 ...
- Flask实战第55天:cms轮播图上传到七牛功能完成
登录七牛云,进入“对象存储”, 新建存储空间(Bucket), 我创建的空间命名为flask-bbs 创建完Bucket,七牛会给我们提供一个测试域名,生产环境中,我们需要绑定自己的域名 在个人面板中 ...
- SSH服务审计工具ssh-audit
SSH服务审计工具ssh-audit SSH服务是常见的远程访问服务.通过对SSH服务进行审计,可以尝试发现对应的漏洞.Kali Linux新增一款SSH服务审计工具ssh-audit.该工具支持 ...
- python 字符串,数学之间的不可描述的关系
首先说一下输入: >>> a=raw_input(" ") 1.234 >>> a '1.234' >>> 可以看到使用raw ...