c# 对INI配置文件的读取操作

此种方法只对如下格式的INI文件起作用
server=localhost
DataBase=pubs
uid=sa
pwd=

/// <summary>
	/// GetDBcfg 的摘要说明。
	/// 读取数据库配置文件
	/// </summary>
	public class GetDBcfg
	{
		//文件路径
		private string path = null;
		private ArrayList list = new ArrayList();

		public GetDBcfg()
		{
			this.path = @"DbCfg.txt";
			if (File.Exists(path)) 
			{
				using (StreamReader sr = File.OpenText(path)) 
				{
					string s = "";
					while ((s = sr.ReadLine()) != null) 
					{
						int start = 0;
						//没有检测到“=”退出本次循环
						if((start=s.IndexOf("="))<0)
						{
							continue;
						}
						//取子串("="以后的值)
						s=s.Substring(start+1);
		
						if(s==null || " ".Equals(s))
						{
							s= "";
						}
						System.Console.WriteLine(s);
						this.list.Add(s);
					}

				}
			}
		}
		
		//参数为文件路径的构造器
		public GetDBcfg(string path)
		{
			this.path = path;
			if (File.Exists(path)) 
			{
				using (StreamReader sr = File.OpenText(path)) 
				{
					string s = "";
					while ((s = sr.ReadLine()) != null) 
					{
						int start = s.IndexOf("=");
						s=s.Substring(start+1);
						if(s==null || " ".Equals(s))
						{
							s= "";
						}
						System.Console.WriteLine(s);
						this.list.Add(s);						
					}
				}
			}

		}
		/// <summary>
		/// 属性
		/// </summary>

		//服务器名
		public string serverName
		{
			get
			{
				return (string)this.list[0];
			}
		}

		//数据库
		public string dataBase
		{
			get
			{
				return (string)this.list[1];
			}
		}

		//用户名
		public string uid
		{
			get
			{
				return (string)this.list[2];
			}
		}

		//密码
		public string pwd
		{
			get
			{
				return (string)this.list[3];
			}
		}
	}

你可能感兴趣的:(配置文件)