WebConfig.cs 3.5 KB
using System;
using System.IO;
using System.Xml;

namespace App
{
	/// <summary>
	/// WebConfig 的摘要说明。
	/// </summary>
	public class WebConfig
	{
        private string _WebConfigPath = @"E:\MyCMMI\MyCMMI\WebService\";
		//		private string _WebServiceUrl=@"http://127.0.0.1/WebService/";
		/// <summary>
		/// 用户节点名字
		/// </summary>
		public const string ApplicationConfiguration="ApplicationConfiguration";
        public const string MyCMMIConfiguration = "MyCMMIConfiguration";
		public WebConfig()
		{
			Initialize();
		}

		private void Initialize()
		{

		}

		/// <summary>
		/// 动态设置Web.Config
		/// </summary>
		/// <param name="UseConfigNode">用户节点名</param>
		/// <param name="AddKey">键标识</param>
		/// <param name="AddValue">键值</param>
		/// <returns>True为成功,False为不成功</returns>
		public bool SetWebConfig(string UseConfigNode,string AddKey,string AddValue)
		{
			XmlDocument doc = new XmlDocument();
			try
			{
				//确保 config 文件可写	Benjamin Qin 2003-11-13
				FileAttributes m_fa = File.GetAttributes(_WebConfigPath);
				if((m_fa & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)	//若为只读
					File.SetAttributes(_WebConfigPath,m_fa^FileAttributes.ReadOnly);//则设为非只读
				if((m_fa & FileAttributes.Hidden) == FileAttributes.Hidden)	//若为隐藏
					File.SetAttributes(_WebConfigPath,m_fa^FileAttributes.Hidden);//则设为非隐藏
				doc.Load(_WebConfigPath);
			}
			catch
			{
				return false;
			}
			XmlNode node=doc.DocumentElement;
            string xpath = "descendant::" + UseConfigNode;//string xpath = "descendant::" + UseConfigNode;
			node=node.SelectSingleNode(xpath);
			if(node==null)
			{
				return false;
			}
			else
			{
				xpath="add[@key='" + AddKey + "']";
				node=node.SelectSingleNode(xpath);
				if(node==null)
				{
					node=doc.DocumentElement;
					xpath="descendant::"+UseConfigNode;
					node=node.SelectSingleNode(xpath);
					XmlElement elem = doc.CreateElement("add");
					elem.SetAttribute("key",AddKey);
					elem.SetAttribute("value",AddValue);
					node.AppendChild(elem);
					try
					{
						doc.Save(_WebConfigPath);
					}
					catch
					{
						return false;
					}
					return true;
				}
				else
				{
					node.Attributes["value"].Value=AddValue;
					try
					{
						doc.Save(_WebConfigPath);
					}
					catch
					{
						return false;
					}
					return true;
				}
			}
		}
		
		/// <summary>
		/// 获取Web.Config中用户属性值
		/// </summary>
		/// <param name="UseConfigNode">用户节点名</param>
		/// <param name="AddKey">键标识</param>
		/// <returns>返回键值,如找不到等其他情况返回空</returns>
		public string GetWebConfig(string UseConfigNode,string AddKey)
		{
			XmlDocument doc = new XmlDocument();
			try
			{
				doc.Load(_WebConfigPath);
			}
			catch
			{
				return "";
			}
			XmlNode node=doc.DocumentElement;
			string xpath="descendant::" + UseConfigNode;
			node=node.SelectSingleNode(xpath);
			if(node==null)
			{
				return "";
			}
			xpath="descendant::add[@key='" + AddKey + "']";
			node=node.SelectSingleNode(xpath);
			if(node!=null)
			{
				return node.Attributes["value"].Value;
			}
			else
			{
				return "";
			}
		}

		//		public string WebServiceUrl
		//		{
		//			get
		//			{
		//				return _WebServiceUrl;
		//			}
		//			set
		//			{
		//				_WebServiceUrl=value;
		//			}
		//		}

		/// <summary>
		/// Config 文件的绝对路径,应包含文件名
		/// </summary>
		public string WebConfigPath
		{
			get
			{
				return _WebConfigPath;
			}
			set
			{
				_WebConfigPath=value;
			}
		}
	}
}