App.Config 操作类

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace DataUpper
{
    
abstract class AppConfig
    
{
        
public static string ReadConfig(string strXmlKey)
        
{
            
return System.Configuration.ConfigurationManager.AppSettings[strXmlKey].ToString();
        }

        
public static void SaveConfig(string strXmlKey,string strXmlValue)
        
{
            XmlDocument doc 
= new XmlDocument();

            
string strFileName = AppDomain.CurrentDomain.BaseDirectory.ToString() + "DataUpper.exe.config";
            doc.Load(strFileName);

            XmlNodeList nodes 
= doc.GetElementsByTagName("add");

            
for (int i = 0; i < nodes.Count; i++)
            
{
                XmlAttribute xmlAtt 
= nodes[i].Attributes["key"];

                
if (xmlAtt.Value == strXmlKey)
                
{
                    xmlAtt 
= nodes[i].Attributes["value"];
                    xmlAtt.Value 
= strXmlValue;
                    
break;
                }

            }


            doc.Save(strFileName);
        }

    }

}

原文地址:https://www.cnblogs.com/zhangpengshou/p/1345052.html