悍匪

回忆过去,展望未来......
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Asp.Net2.0新增/修改/删除 配置文件(*.config)节点

Posted on 2008-06-13 07:58  悍匪  阅读(431)  评论(1)    收藏  举报

最近使用配置文件,需要删除修改,所以找了个类。。。

类库如下:

using System;
using System.Configuration;
using System.Reflection;
using System.Web;
using System.Xml;
public enum ConfigFileType
{
    WebConfig,
    AppConfig
}


/// <summary>
/// Summary description for ReadWriteConfig.
/// </summary>

public class ReadWriteConfig
{
    
public string docName = String.Empty;
    
private XmlNode node = null;
    
private int _configType;
    
public int ConfigType
    
{
        
get return _configType; }
        
set { _configType = value; }
    }


    
SetValue

    
saveConfigDoc

    
removeElement

    
modifyElement

    
loadConfigDoc
}


System.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  
<appSettings>
    
<add key="UpLoadImage" value="http://localhost:4332/Admin/" />
    
<add key="EditUpLoadImage" value="JPG|GIF|PNG" />
    
<add key="UpLoadFiles" value="http://***:8008/UpLoadFiles/" />
    
<add key="FileTypeAllow" value="JPG|GIF|TXT|DOC" />
    
<add key="UpLoadSmallImage" value="http://***:8008/Smallimages/" />
    
<add key="UpLoadBigImages" value="http://***:8008/Bigimages/" />
    
<add key="DataBackPath" value="D:\SQL_Data\data_bck_test" />
    
<add key="CacheLength" value="1|h" />
    
<add key="FilesCount" value="5" />
  
</appSettings>
  
<connectionStrings />
  
<system.web>
  
</system.web>
</configuration>

使用:
读:
ReadWriteConfig config = new ReadWriteConfig();
config.ConfigType 
= (int)ConfigFileType.WebConfig;
txtImageTypeAllow.Text 
= config.readConfigDoc("EditUpLoadImage");
写:
bool b = false;
ReadWriteConfig config 
= new ReadWriteConfig();
config.ConfigType 
= (int)ConfigFileType.WebConfig;
= config.SetValue("UpLoadFiles", txtFilePath.Text);
删除使用 removeElement(...)函数