C#读取properties文件
来源:互联网 发布:外交豁免权知乎 编辑:程序博客网 时间:2024/06/05 16:36
java中有读取*.properties的专有类,C#中却没能找到.今天写了一个基本的Class,并解决了hashtable中按照读的顺序输出问题,供大家参考.
/// <summary>
/// 类名:FileProperties
/// 描述:操作*.Properties文件
/// 作者:Michael
/// 创建时间:2008-03-11
/// </summary>
public class FileProperties : System.Collections.Hashtable
{
private ArrayList keys = new ArrayList();
private String fileName = string.Empty; //要读写的Properties文件名
/// <summary>
/// 构造函数
/// </summary>
/// <param name="fileName">文件名</param>
public FileProperties(String fileName)
{
this.setFileName(fileName);
load(fileName);
}
private void setFileName(string filePath)
{
this.fileName = filePath;
}
/// <summary>
/// 重写Add方法,实现按添加顺序排列
/// </summary>
/// <param name="key">key</param>
///<param name="value">value</param>
/// <returns></returns>
public override void Add(object key, object value)
{
base.Add (key, value);
keys.Add (key);
}
public override ICollection Keys
{
get
{
return keys;
}
}
/// <summary>
/// 导入文件
/// </summary>
/// <param name="filePath">要导入的文件</param>
/// <returns></returns>
public 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">要保存的Properties文件</param>
/// <returns></returns>
public void save(string filePath)
{
if(File.Exists(filePath))
{
File.Delete(filePath);
}
FileStream fileStream = File.Create(filePath);
StreamWriter sw = new StreamWriter(fileStream);
foreach (object item in keys)
{
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();
}
}
- C#读取properties文件
- C#读取properties文件
- C#读取java的Properties文件
- Properties读取properties文件
- Properties 读取 Properties文件
- Properties文件读取 Message.properties
- 读取中文Properties文件
- properties 文件的读取
- Java读取Properties文件
- java读取.properties文件
- struts读取.properties文件
- ejb读取properties文件
- 读取properties文件-PropertyResourceBundle
- 读取properties文件
- 读取属性文件*.properties
- java读取.properties文件
- 读取properties文件
- 读取Properties文件信息
- SEO优化每天网站日志分析
- 转 A2W、W2A、T2A、T2W 等宏
- 如何实现添加多条信息到GridView中,而一次保存到数据库,不被页面刷新而丢失数据呢?
- 深入研究java.lang.Class类
- 某dll程序被删自动还原代码
- C#读取properties文件
- 【Android】Web开发之通知栏下载更新APP
- C语言处理BMP图像
- 求解SDP问题—使用SeDuMi和YALMIP
- SVD分解在文本分类中的应用
- 最佳路由选择原则
- 我看ios7
- 各种线性排序记录(计数排序,基数排序,桶排序) 及 希尔排序
- udp广播、多播