c# 读取配置文件方法

问题:在做c#项目开发的时候,如果有一些变量是我们后期需要维护或者改变的,像这样的变量我们就不能再开发的时候在代码中写死了,后期维护困难。

解决方法:一般来说,简单的方法就是将变量配置在配置文件中,方便后期维护。

那么问题来了,有时配置文件的路径地址等的不同,导致去配置文件经常读不到,在一些大公司,研发环境和测试环境部署不同时,配置文件的读取就更加不方便了,因此,给大家分享一下读取配置文件基本方法,基本类。代码如下;

举个简单的例子,比如你的配置文件名字是:FlightChangeAutomation.config

代码:

using System;

using System.Collections.Generic;

using System.Configuration;

using System.IO;

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;

/// <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"/>

引用:https://www.cnblogs.com/skyfreedom/p/5455404.html