请问在C/S系统中如何设置配置文件
来源:互联网 发布:淘宝美工提成方案 编辑:程序博客网 时间:2024/04/28 20:25
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; } }
- 请问在C/S系统中如何设置配置文件
- 请问在MDI中,如何设置子Form的大小??
- 请问在CODEVISION AVR C中如何在函数中传递数组的地址?
- 请问在CODEVISION AVR C中如何在函数中传递数组的地址?
- 请问在flex中如何调用GIFPlayer
- 请问如何在GDI+中实现TLOGFONT逻辑字体的任意设置???
- 请问如何在SQL SERV 2K中设置锁--很实用的一篇转了
- 请问:在c++builder中Teechart的LoadChartFromFile函数如何调用?
- 请问一下,如何在C#Windows编程中创建可停靠的工具条……
- 在配置文件中设置全局变量
- 请问在javascript中如何实现字符串编码转换
- 请问如何在水晶报表中控制打印机走纸?
- 请问在C#中如何实现声音报警?
- 请问在oracle中如何创建临时表
- [VB.NET]请问如何在dataGrid中显示搜索结果?
- [VB.NET]请问如何在datagridview中插入button
- 请问,如何在NSTableView 的单元格中添加图片呢?
- 请问:如何在文本文件中追加一列?awk字符串比较
- C#能否象JAVA那样根据类名建立Class类型的对象
- 开篇大吉
- 如何在.NET框架下创建Access数据库和表
- 怎样用C#打开Word文档
- Hilary Stagg 不死传说
- 请问在C/S系统中如何设置配置文件
- 英文地址的翻译
- 浪漫国名
- Nested DataGrid/DataList
- 某大牛的车牌
- 数组过滤重复字符
- 用Visual C#创建Windows服务程序
- 在aspx发送前记录下生成的html
- 访问远程access数据库总弹出的对话框