请问在C/S系统中如何设置配置文件

System.Configuration.ConfigurationSettings.GetConfig("ApplicationConfiguration");
or
Runtime Web.config / App.config Editing
http://www.eggheadcafe.com/articles/20030907.asp
------------------------------------------------------------------------------

using System; using System.Xml; using System.Configuration; using System.Collections; using System.Reflection; using System.Diagnostics ; public enum ConfigFileType { WebConfig , AppConfig } public class AppConfig : System.Configuration.AppSettingsReader { public string docName = String.Empty; private XmlNode node=null; private int _configType;
public int ConfigType { get { return _configType; } set { _configType=value; } } public bool SetValue(string key, string value) { XmlDocument cfgDoc = new XmlDocument(); loadConfigDoc(cfgDoc); // retrieve the appSettings node node = cfgDoc.SelectSingleNode("//appSettings"); if( node == null ) { throw new System.InvalidOperationException( "appSettings section not found"); } try { // XPath select setting "add" element that contains this key XmlElement addElem= (XmlElement)node.SelectSingleNode("//add[@key='" +key +"']") ; if (addElem!=null) { addElem.SetAttribute("value",value); } // not found, so we need to add the element, key and value else { XmlElement entry = cfgDoc.CreateElement("add"); entry.SetAttribute("key",key); entry.SetAttribute("value",value); node.AppendChild(entry); } //save it saveConfigDoc(cfgDoc,docName); return true; } catch { return false; } } private void saveConfigDoc(XmlDocument cfgDoc,string cfgDocPath) { try { XmlTextWriter writer = new XmlTextWriter( cfgDocPath , null ); writer.Formatting = Formatting.Indented; cfgDoc.WriteTo( writer ); writer.Flush(); writer.Close(); return; } catch { throw; } } public bool removeElement ( string elementKey) { try { XmlDocument cfgDoc = new XmlDocument(); loadConfigDoc(cfgDoc); // retrieve the appSettings node node = cfgDoc.SelectSingleNode("//appSettings"); if( node == null ) { throw new System.InvalidOperationException( "appSettings section not found"); } // XPath select setting "add" element that contains this key to remove node.RemoveChild( node.SelectSingleNode("//add[@key='" +elementKey +"']") ); saveConfigDoc(cfgDoc,docName); return true; } catch { return false; } } private XmlDocument loadConfigDoc( XmlDocument cfgDoc ) { // load the config file if( Convert.ToInt32(ConfigType)==Convert.ToInt32(ConfigFileType.AppConfig)) { docName= ((Assembly.GetEntryAssembly()).GetName()).Name; docName += ".exe.config"; } else { docName=System.Web.HttpContext.Current.Server.MapPath("web.config"); } cfgDoc.Load( docName ); return cfgDoc; } }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值