问题:在作c#项目开发的时候,若是有一些变量是咱们后期须要维护或者改变的,像这样的变量咱们就不能再开发的时候在代码中写死了,后期维护困难。数据库
解决方法:通常来讲,简单的方法就是将变量配置在配置文件中,方便后期维护。c#
那么问题来了,有时配置文件的路径地址等的不一样,致使去配置文件常常读不到,在一些大公司,研发环境和测试环境部署不一样时,配置文件的读取就更加不方便了,所以,给你们分享一下读取配置文件基本方法,基本类。代码以下;app
举个简单的例子,好比你的配置文件名字是:FlightChangeAutomation.config函数
代码:测试
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;spa
namespace Better517Na.FlightChangeAutomation.Factory
{
/// <summary>
/// 配置文件监听器类:监听配置文件变动
/// </summary>
public class ConfigMonitor
{
/// <summary>
/// 链接配置 Configuration
/// </summary>
private static Configuration flightChangeAutomationConfig = null;代理
/// <summary>
/// 链接配置文件 ExeConfigurationFileMap
/// </summary>
private static ExeConfigurationFileMap map = null;orm
/// <summary>
/// 构造函数:初始化读取配置文件,为文件注册侦听器
/// </summary>
static ConfigMonitor()
{
MonitorConfigFile();
InitConnectionConfig();
}对象
/// <summary>
/// 配置文件变动代理 delegate
/// </summary>
public delegate void EventHandlerAfterConfigModify();事件
/// <summary>
/// 配置文件变动事件 event
/// </summary>
public static event EventHandlerAfterConfigModify ConfigModifyInfoEvent;
/// <summary>
/// 配置对象 Configuration
/// </summary>
public static Configuration FlightChangeAutomationConfig
{
get
{
return flightChangeAutomationConfig;
}
set
{
flightChangeAutomationConfig = value;
}
}
/// <summary>
/// 建立配置文件发动监听器
/// </summary>
public static void MonitorConfigFile()
{
FileSystemWatcher fileWatcher = new FileSystemWatcher();
fileWatcher.Path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\";
fileWatcher.Filter = "FlightChangeAutomation.config";
fileWatcher.NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.LastWrite | NotifyFilters.FileName;
// Add event handlers.
fileWatcher.Changed += new FileSystemEventHandler(OnChanged);
fileWatcher.Created += new FileSystemEventHandler(OnChanged);
fileWatcher.Deleted += new FileSystemEventHandler(OnChanged);
fileWatcher.Renamed += new RenamedEventHandler(OnChanged);
// Begin watching.
fileWatcher.EnableRaisingEvents = true;
}
/// <summary>
/// 向订阅者发布信息(配置文件变动时)
/// </summary>
private static void RaiseEvent()
{
if (ConfigModifyInfoEvent != null)
{
ConfigModifyInfoEvent();
}
}
/// <summary>
/// 初始化数据库链接配置
/// </summary>
private static void InitConnectionConfig()
{
// 读取配置文件进行全部的链接初始化操做
map = new ExeConfigurationFileMap();
map.ExeConfigFilename = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\FlightChangeAutomation.config"; ////(配置文件名字)
flightChangeAutomationConfig = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
}
/// <summary>
/// 文件改变:从新加载配置
/// </summary>
/// <param name="source">事件源 object</param>
/// <param name="e">事件参数 FileSystemEventArgs</param>
private static void OnChanged(object source, FileSystemEventArgs e)
{
InitConnectionConfig();
RaiseEvent();
}
}
}
以上类就能够实现对配置文件的读取。
读取方法:
ConfigMonitor.FlightChangeAutomationConfig.AppSettings.Settings[note] (note就是配置文件的key)
ConfigMonitor.FlightChangeAutomationConfig.AppSettings.Settings[note].Value.ToString();
配置文件格式为:
<add key="UpdateStatusSpanDeptID" value="17111"/>