APP.config/web.config编辑类

 

None.gif using  System;
None.gif
using  System.Configuration;
None.gif
using  System.Reflection;
None.gif
using  System.Web;
None.gif
using  System.Xml;
None.gif
None.gif
public   enum  ConfigFileType
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    WebConfig,
InBlock.gif    AppConfig
ExpandedBlockEnd.gif}

None.gif
None.gif
namespace  fenghua.Configuration
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// AppConfig 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class AppConfig : AppSettingsReader
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{  
InBlock.gif        
public string docName = String.Empty; 
InBlock.gif        
private XmlNode node=null;    
InBlock.gif        
private int _configType;  
InBlock.gif        
public int ConfigType
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gifreturn _configType; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{ _configType=value; }
ExpandedSubBlockEnd.gif        }
  
InBlock.gif
InBlock.gif        
public bool SetValue(string key, string value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            XmlDocument cfgDoc 
= new XmlDocument();
InBlock.gif            loadConfigDoc(cfgDoc);   
InBlock.gif            
// retrieve the appSettings node   
InBlock.gif
            node =  cfgDoc.SelectSingleNode("//appSettings");      
InBlock.gif            
if( node == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new InvalidOperationException( "appSettings section not found");
ExpandedSubBlockEnd.gif            }
     
InBlock.gif            
try  
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{   
InBlock.gif                
// XPath select setting "add" element that contains this key       
InBlock.gif
                XmlElement addElem= (XmlElement)node.SelectSingleNode("//add[@key='" +key +"']") ;
InBlock.gif                
if(addElem!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    addElem.SetAttribute(
"value",value);
ExpandedSubBlockEnd.gif                }
    
InBlock.gif                    
// not found, so we need to add the element, key and value   
InBlock.gif
                else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    XmlElement entry 
= cfgDoc.CreateElement("add");    entry.SetAttribute("key",key);    entry.SetAttribute("value",value);     node.AppendChild(entry);
ExpandedSubBlockEnd.gif                }
   
InBlock.gif                
//save it   
InBlock.gif
                saveConfigDoc(cfgDoc,docName);   
InBlock.gif                
return true;  
ExpandedSubBlockEnd.gif            }
  
InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }
 
ExpandedSubBlockEnd.gif        }
  
InBlock.gif        
InBlock.gif        
private void saveConfigDoc(XmlDocument cfgDoc,string cfgDocPath)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                XmlTextWriter writer 
= new XmlTextWriter( cfgDocPath , null ); 
InBlock.gif                writer.Formatting 
= Formatting.Indented;    
InBlock.gif                cfgDoc.WriteTo( writer );    
InBlock.gif                writer.Flush(); 
InBlock.gif                writer.Close();  
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }
  
InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }
 
InBlock.gif        
InBlock.gif        
public bool removeElement ( string elementKey) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
try  
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{   
InBlock.gif                XmlDocument cfgDoc 
= new XmlDocument(); 
InBlock.gif                loadConfigDoc(cfgDoc);  
InBlock.gif                
// retrieve the appSettings node  
InBlock.gif
                node =  cfgDoc.SelectSingleNode("//appSettings");   
InBlock.gif                
if( node == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
throw new InvalidOperationException( "appSettings section not found");
ExpandedSubBlockEnd.gif                }
     
InBlock.gif                
// XPath select setting "add" element that contains this key to remove      
InBlock.gif
                node.RemoveChild( node.SelectSingleNode("//add[@key='" +elementKey +"']") ); 
InBlock.gif                saveConfigDoc(cfgDoc,docName);  
InBlock.gif                
return true
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }
 
ExpandedSubBlockEnd.gif        }
  
InBlock.gif        
InBlock.gif        
private XmlDocument loadConfigDoc( XmlDocument cfgDoc ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{   
InBlock.gif            
// load the config file   
InBlock.gif
            if(  Convert.ToInt32(ConfigType)==Convert.ToInt32(ConfigFileType.AppConfig))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                docName
= ((Assembly.GetEntryAssembly()).GetName()).Name;   
InBlock.gif                docName 
+=   ".exe.config";
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                docName
=HttpContext.Current.Server.MapPath("web.config");
ExpandedSubBlockEnd.gif            }
  
InBlock.gif            cfgDoc.Load( docName );  
InBlock.gif            
return cfgDoc;
ExpandedSubBlockEnd.gif        }
 
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

使用方法:

1.app.config

  private void Form1_Load(object sender, System.EventArgs e)
  {
   AppConfig config = new AppConfig(); 
   config.ConfigType= (int)ConfigFileType.AppConfig;

  
   bool   bln=  (bool)( config.GetValue( "Boolean", typeof( bool ) ) );
   string  str= (string)( config.GetValue( "String", typeof( string ) ) );
   DateTime date = (DateTime)( config.GetValue( "DateTime", typeof( DateTime ) ) );
  

   bln  = !bln;
   str  = str + "-" + "there";
   date = date.AddDays( 1 );
  
   config.SetValue( "Boolean", bln.ToString() );
   config.SetValue( "String", str );
   config.SetValue( "DateTime", date.ToShortDateString() ); 
   config.SetValue("testing", "1234506");

   config.SetValue("howdy", "there");
   string  str2= (string)( config.GetValue( "howdy", typeof( string ) ) );
   
  
   textBox1.Text+= bln +"         ";
   textBox1.Text+= str2+"      " ;
   textBox1.Text+= date.ToShortDateString()+"       " ;
   // uncomment to see element removed
   //config.removeElement("howdy");

 }

2.修改web.config:

 

 
 

  private void Page_Load(object sender, System.EventArgs e)
  {
 
   AppConfig config = new AppConfig();
   //config.ConfigType = (int)ConfigFileType.WebConfig;
  
   
   bool   bln=  (bool)( config.GetValue( "Boolean", typeof( bool ) ) );
   string  str= (string)( config.GetValue( "String", typeof( string ) ) );
   DateTime date = (DateTime)( config.GetValue( "DateTime", typeof( DateTime ) ) );
  
   bln  = !bln;
   str  = str + "-" + "there";
   date = date.AddDays( 1 );
  
   config.SetValue( "Boolean", bln.ToString() );
   config.SetValue( "String", str );
   config.SetValue( "DateTime", date.ToShortDateString() ); 
   config.SetValue("testing", "1234506");
   config.SetValue("howdy", "there");
              string  str2= (string)( config.GetValue( "howdy", typeof( string ) ) );
   
  
   Response.Write( bln + "<BR>" );
   Response.Write( str2  + "<BR>");
   Response.Write( date.ToShortDateString() + "<BR>" );
   // uncomment to see element removed
   //config.removeElement("howdy");


  }

转载于:https://www.cnblogs.com/zhengmaoch/archive/2005/08/05/208081.html

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值