从网上找的配置文件类,比我原来的好,能用在winform和webform项目中

能用在两种项目中,比较不错
using System;
using System.Configuration;
using System.Reflection;
using System.Web;
using System.Xml;

public enum ConfigFileType


namespace TexSelect
{
    
/**//// <summary>
    
/// 配置文件操作类
    
/// </summary>

    public class AppConfig : AppSettingsReader
    

}

使用方法:
string sqlConnText="";

            AppConfig appconfig
=new AppConfig();
            appconfig.ConfigType
=(int)ConfigFileType.WebConfig;

            sqlConnText
=(string)appconfig.GetValue("SQLConnectionString",sqlConnText.GetType());

设置方法:
项目为web项目,则配置信息存在web.config文件中,使用时:appconfig.ConfigType=(int)ConfigFileType.WebConfig;
项目为win项目,则配置信息存在app.config文件中,使用时:appconfig.ConfigType=(int)ConfigFileType.AppConfig;

配置文件示例:
不论在web.config中还是在app.config中都放在<configuration></configuration>之间,格式如下:
 <appSettings>

     
<add key="SQLConnectionString" value="Server=127.0.0.1;database=tex;uid=sa;pwd=sa" />
         
  
</appSettings>

原文地址:https://www.cnblogs.com/telephoner/p/236929.html