c#读properties文件
@(编程)
properties文件
MONGO_URL = mongodb://172.16.20.3/srtc_dc
CURRENT_VERSION = 2.0
IS_AUTO_UPDATE = no
REMOTE_UPDATE_FILE = http://192.168.1.121:8080/ClientBin.zip
REMOTE_VERSION_FILE = http://192.168.1.121:8080/version
PROGRAM_PATH = E:\dc\ClientBin\Wims.Mili.DC.Client.SYS.exe
源码
using System;
using System.Collections;
using System.IO;
namespace Wisdombud.Util
{
public class PropertyOper : System.Collections.Hashtable
{
private string fileName = "";
private ArrayList list = new ArrayList();
public ArrayList List
{
get { return list; }
set { list = value; }
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="fileName">要读写的properties文件名</param>
public PropertyOper(string fileName)
{
this.fileName = fileName;
this.Load(fileName);
}
/// <summary>
/// 重写父类的方法
/// </summary>
/// <param name="key">键</param>
/// <param name="value">值</param>
public override void Add(object key, object value)
{
base.Add(key, value);
list.Add(key);
}
public void Update(object key, object value)
{
base.Remove(key);
list.Remove(key);
this.Add(key, value);
}
public override ICollection Keys
{
get
{
return list;
}
}
/// <summary>
/// 加载文件
/// </summary>
/// <param name="filePath">文件路径</param>
private void Load(string filePath)
{
char[] convertBuf = new char[1024];
int limit;
int keyLen;
int valueStart;
char c;
string bufLine = string.Empty;
bool hasSep;
bool precedingBackslash;
using (StreamReader sr = new StreamReader(filePath))
{
while (sr.Peek() >= 0)
{
bufLine = sr.ReadLine();
limit = bufLine.Length;
keyLen = 0;
valueStart = limit;
hasSep = false;
precedingBackslash = false;
if (bufLine.StartsWith("#"))
keyLen = bufLine.Length;
while (keyLen < limit)
{
c = bufLine[keyLen];
if ((c == '=' || c == ':') & !precedingBackslash)
{
valueStart = keyLen + 1;
hasSep = true;
break;
}
else if ((c == ' ' || c == '\t' || c == '\f') & !precedingBackslash)
{
valueStart = keyLen + 1;
break;
}
if (c == '\\')
{
precedingBackslash = !precedingBackslash;
}
else
{
precedingBackslash = false;
}
keyLen++;
}
while (valueStart < limit)
{
c = bufLine[valueStart];
if (c != ' ' && c != '\t' && c != '\f')
{
if (!hasSep && (c == '=' || c == ':'))
{
hasSep = true;
}
else
{
break;
}
}
valueStart++;
}
string key = bufLine.Substring(0, keyLen);
string values = bufLine.Substring(valueStart, limit - valueStart);
if (key == "")
key += "#";
while (key.StartsWith("#") & this.Contains(key))
{
key += "#";
}
this.Add(key, values);
}
}
}
/// <summary>
/// 保存文件
/// </summary>
/// <param name="filePath">要保存的文件的路径</param>
public void Save()
{
string filePath = this.fileName;
if (File.Exists(filePath))
{
File.Delete(filePath);
}
FileStream fileStream = File.Create(filePath);
StreamWriter sw = new StreamWriter(fileStream);
foreach (object item in list)
{
String key = (String)item;
String val = (String)this[key];
if (key.StartsWith("#"))
{
if (val == "")
{
sw.WriteLine(key);
}
else
{
sw.WriteLine(val);
}
}
else
{
sw.WriteLine(key + "=" + val);
}
}
sw.Close();
fileStream.Close();
}
}
}
应用
public static void ReadConfig()
{
PropertyOper po = new PropertyOper("update.properties");
GlobalValues.MONGO_URL = po["MONGO_URL"].ToString();
GlobalValues.IS_AUTO_UPDATE = po["IS_AUTO_UPDATE"].ToString().ToLower().Equals("yes") ? true : false;
GlobalValues.REMOTE_UPDATE_FILE = po["REMOTE_UPDATE_FILE"].ToString();
GlobalValues.REMOTE_VERSION_FILE = po["REMOTE_VERSION_FILE"].ToString();
GlobalValues.PROGRAM_PATH = po["PROGRAM_PATH"].ToString();
FileInfo fi = new FileInfo(GlobalValues.PROGRAM_PATH);
GlobalValues.PROGRAM_FOLDER = fi.Directory.FullName;
GlobalValues.PROGRAM_NAME = fi.Name.Replace(".exe", "");
GlobalValues.CURRETN_VERSION = po["CURRENT_VERSION"].ToString();
GetIP();
DaoFactory.Init();
}
c#读properties文件的更多相关文章
- Java读properties文件中文乱码问题的解决方法
java读properties文件,包含中文字符的主要有两种: 1.key中包含中文字符的(value中也有可能包含) 2.key中不包含中文字符的(value中有可能包含) 1.key中包含中文字符 ...
- java读properties文件 乱码
java读properties文件,包含中文字符的主要有两种: 1.key中包含中文字符的(value中也有可能包含) 2.key中不包含中文字符的(value中有可能包含) 1.key中包含中文字符 ...
- 读写properties文件
1. 读properties文件 Properties props = new Properties(); try { InputStream in = new FileInputStream(&qu ...
- Java 读Properties
import java.io.*; import java.util.Properties; public class Study { public static void main(String[] ...
- ResourceBundle读取中文properties文件问题
昨天遇到一个问题,用ResourceBundle读取中文字符串资源文件时,死活读不出来. 一开始以为是文件路径不对,后来发现如果默认properties文件时英文就没问题.我的项目代码是在src目录下 ...
- Properties文件及与之相关的System.getProperties操作(转)
如何使用Java读写系统属性? 读: 简述properties文件的结构和基本用法结构:扩展名为properties的文件,内容为key.value的映射,例如"a=2" 示例用到 ...
- 解决Eclipse中.properties文件中文乱码问题
在.properties文件写注释时,发现中文乱码了,由于之前在idea中有见设置.properties文件的编码类型,便找了找乱码原因 在中文操作系统中,Eclipse中的Java类型文件的编码的默 ...
- SpringMVC+HibernateValidator,配置在properties文件中的错误信息回显前端页面出现中文乱码
问题: 后台在springMVC中使用hibernate-validator做参数校验的时候(validator具体使用方法见GOOGLE),用properties文件配置了校验失败的错误信息.发现回 ...
- Configutation读取properties文件信息
commons configuration可以很方便的访问配置文件和xml文件中的的内容.Commons Configuration 是为了提供对属性文件.XML文件.JNDI资源.来自JDBC Da ...
随机推荐
- Android--动态添加控件
[html] [html] package com.mrzhu.edittest; import android.app.Activity; import ...
- Qt之QLCDNumber
简述 QLCDNumber控件用于显示一个LCD数字. 它可以显示几乎任意大小的数字.可以显示十进制.十六进制.八进制或二进制数.很容易使用display()槽连接到数据源,这个槽可以被任何五个参数类 ...
- 51nod1434 区间LCM
将n!标准分解.m!/n!必定需要包含n!的分解式.对于每个质数枚举最小的答案,然后总的取最大. #include<cstdio> #include<cstring> #inc ...
- HDU 5264 pog loves szh I (字符串,水)
题意:设有两个串A和B,现将B反转,再用插入的形式合成一个串.如:A:abc B:efg:反转B先,变gfe:作插入,agbfce.现在给出一个串,要求还原出A和B. 思路:扫一遍O(n),串A在 ...
- web服务器的相关资料 ngix
OpenResty:官方网站 http://openresty.org/cn/index.html 利用nginx+lua+memcache实现灰度发布 http://www.cnblogs.com ...
- 【英语】Bingo口语笔记(66) - 美式发音特点
- delphi中通过CreateOleObject操控Word
http://blog.csdn.net/csm2432/article/details/7692443
- 【c++】输出 0000,0001,0002,0003,0004...这样的字符串
#include <iostream> #include <iomanip> ; ){ stringstream buf; buf <<setfill()<& ...
- 类 .xml
pre{ line-height:1; color:#1e1e1e; background-color:#d2d2d2; font-size:16px;}.sysFunc{color:#627cf6; ...
- Trie树也称字典树
Trie树 Trie树也称字典树,因为其效率很高,所以在在字符串查找.前缀匹配等中应用很广泛,其高效率是以空间为代价的. 一.Trie树的原理 利用串构建一个字典树,这个字典树保存了串的公共前缀信息, ...