C#读取配置文件源代码
来源:互联网 发布:python sys.exit 参数 编辑:程序博客网 时间:2024/04/29 13:23
example1:mysql.ini
[mysql]
username=root;
//注释
password=123456
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mysql
///////////////////////////////////////////////////////////////////////////////////////////////////////////
FileAssistant.cs
//////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace FileAssistant
{
public class Assistant
{
private String path,section="section1";
private StreamWriter writer=null;
private StreamReader reader=null;
private bool isSection = false,isReader=false,isAppend;
public String Path
{
set
{
path = value;
}
get
{
return path;
}
}
public String Section
{
set
{
section = value;
}
get
{
return section;
}
}
public Assistant()
{
}
public void parse(String path, bool isAppend)
{
this.path = path;
this.isAppend = isAppend;
}
public void put(String Key, String Value)
{
if (isAppend)
{
writer = File.AppendText(path);
}
else
{
writer = File.CreateText(path);
}
if (!isSection)
{
writer.WriteLine("["+section+"]");
}
writer.WriteLine(Key+"="+Value);
writer.Flush();
writer.Close();
isSection = true;
}
public String get(String Key)
{
if (File.Exists(path))
{
try
{
String line = "";
String Value = "";
reader = File.OpenText(path);
line = reader.ReadLine();
while (line != null)
{
if (line.IndexOf("[" + section + "]") >= 0)
{
continue;
}
else
if (line.IndexOf(";") >= 0 || line.IndexOf("#") >= 0)
{
continue;
}
else
if (line.IndexOf(Key) >= 0 && line.IndexOf(";") < 0 && line.IndexOf("#") < 0)
{
Value = line.Substring(Key.Length + "=".Length);
}
line = reader.ReadLine();
}
}
catch (Exception e)
{
reader.Close();
throw new Exception(e.Message);
}
}
else
{
throw new FileNotFoundException(path+"未找到,请确认文件是否存在!");
}
reader.Close();
return Value;
}
public void release()
{
if (isSection)
{
writer.Close();
}
else if(isReader)
{
reader.Close();
}
reader = null;
writer = null;
}
~Assistant()
{
if (isSection)
{
writer.Close();
}
else if (isReader)
{
reader.Close();
}
reader = null;
writer = null;
}
}
}
///////////////////////////////////////////////////////////////////
Test.cs
///////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FileAssistant;
namespace AssistantTest
{
class Program
{
static void Main(string[] args)
{
Assistant file = new Assistant();
file.parse("c://mysql.ini",true);
/**file.Section = "mysql";
file.put("username","root");
file.put("password", "123456");
file.put("driver", "com.mysql.jdbc.Driver");
file.put("url", "jdbc:mysql://localhost:3306/mysql");*/
Console.WriteLine(file.get("username"));
Console.WriteLine(file.get("password"));
Console.WriteLine(file.get("driver"));
Console.WriteLine(file.get("url"));
file.release();
Console.ReadLine();
}
}
}
result:
root
123456
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/mysql
- C#读取配置文件源代码
- 读取配置文件源代码[linux c]
- C#读取XML配置文件
- C#配置文件读取
- C#读取ini配置文件
- C#读取配置文件详解
- C#读取配置文件详解
- C# 读取ini配置文件
- c# 读取TIniFile配置文件
- C#读取配置文件详解
- 读取配置文件 C#
- C#中读取配置文件
- C#读取XML配置文件
- c#读取配置文件
- C# 读取ini配置文件
- C#读取配置文件
- C# 读取ini配置文件
- C# 读取ini配置文件
- winavi的DVD刻录方法
- Adaptive Collaborative Work(ACW) 自适应协同工作 109 of 281 and Software as a Service(202)
- SQL SERVER数据库开发之存储过程应用
- 使用apt-spy寻找Debian最快的源
- Head First C# 中文版 第九章 读写文件 page396
- C#读取配置文件源代码
- SQL Server DO's and DON'Ts
- 搬家了,梳理一下自己的生活
- emacs command 大全
- emacs command 解释
- javascript 事件冒泡
- 如何教小弟弟小妹妹进入软件这行
- asp.net 中屏蔽鼠标右键、Ctrl+N、Shift+F10、F11、F5刷新、退格键
- Linux必学的60个基础命令