config文件操作类

Code
using System; 
using System.Xml; 
using System.Configuration; 
using System.Collections; 
using System.Reflection; 
using System.Diagnostics;
namespace InstallSetting 

    
public class AppConfigOper : System.Configuration.AppSettingsReader 
    { 
        
public string docName = String.Empty; 
        
private XmlNode node = null;

        
public AppConfigOper(string file) 
        { 
            docName 
= file; 
        }
        
public bool SetValue(string key, string value) 
        { 
            XmlDocument cfgDoc 
= new XmlDocument(); 
            cfgDoc.Load(docName); 
            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
            } 
        }

        
public bool SetWebServiceUrl(string Url) {
            XmlDocument cfgDoc 
= new XmlDocument();
            cfgDoc.Load(docName);
            node 
= cfgDoc.SelectSingleNode("//IDVerifyDemo.Properties.Settings");
            
if (node == null)
            {
                
throw new System.InvalidOperationException("setting section not found");
            }
            
try
            {
                
// XPath select setting "add" element that contains this key       
                XmlElement addElem = (XmlElement)node.SelectSingleNode("//setting[@name='IDVerifyDemo_SendAccessInfo_VisitService']");
                
if (addElem != null)
                {
                   
// addElem.SelectSingleNode("value") .Value = Url;
                    addElem.SelectSingleNode("value").InnerText = Url;
                    
//save it 
                    saveConfigDoc(cfgDoc, docName);
                }
                
return true;
            }
            
catch(Exception )
            {
                
return false;
            } 
        }

        
//<connectionStrings>
    
//<add name="connString" connectionString="Provider=Microsoft.Jet.OleDb.4.0;Data Source=d:\Card.mdb;User ID='admin';Jet OLEDB:Database Password='123'"/>
  
//</connectionStrings>

        
public bool  setDataSource(string url){
            
try
            {

                XmlDocument cfgDoc 
= new XmlDocument();
                XmlNode node 
= null;
                cfgDoc.Load(docName);
                node 
= cfgDoc.SelectSingleNode("//connectionStrings/add");
                XmlAttribute xmlattr 
= node.Attributes["connectionString"];
                xmlattr.Value 
= url;
                saveConfigDoc(cfgDoc, docName);
                
return true;
            }
            
catch(Exception ex)
            {
                
return false;
            }
         }

        
public bool SetLogPath(string Url)
        {
            XmlDocument cfgDoc 
= new XmlDocument();
            cfgDoc.Load(docName);
            node 
= cfgDoc.SelectSingleNode("//log4net/appender/file");
            
if (node == null)
            {
                
throw new System.InvalidOperationException("log4net not found");
            }
            
try
            {

                
if (node != null)
                {
                    
// addElem.SelectSingleNode("value") .Value = Url;
                    XmlAttribute xmlattr = node.Attributes["value"];
                    xmlattr.Value 
= Url;
                    
//save it 
                    saveConfigDoc(cfgDoc, docName);
                }
                
return true;
            }
            
catch (Exception)
            {
                
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
            } 
        } 
    } 


你可能感兴趣的:(config)