项目中总有些配置是需要动态配置的,比如测试环境和生产环境的不同,这里介绍asp.net mvc 项目调用xml格式的配置文件。
1、创建配置文件
在VideoMonitorPlatform项目下建立“Configs”文件夹,在文件夹下建立“Customer.config”配置文件,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<CustomerConfig>
<!-- NVR的IP -->
<add key="NvrIp" value="192.168.1.100"/>
<!-- NVR的端口号 -->
<add key="NvrPort" value="37777"/>
<!-- NVR的登录用户名 -->
<add key="NvrUserName" value="admin"/>
<!-- NVR的登录密码 -->
<add key="NvrPassword" value="admin123"/>
<!-- 截图图片文件保存位置 -->
<add key="CaptureSavePath" value="D:\\files\\VideoMonitorPlatform\\capture"/>
</CustomerConfig>
2、项目中引入配置文件
在“Web.config”文件中加入如下配置:
<section name="CustomerConfig" type="VideoMonitorPlatform.Models.Config.CustomerConfig, VideoMonitorPlatform"/>
<CustomerConfig configSource="Configs\Customer.config"/>
3、添加读取配置文件类
(1)类KeyValueElementCollection:
using System;
using System.Configuration;
namespace VideoMonitorPlatform.Models.Config
{
/// <summary>
/// 元素集合类
/// <remarks>
/// 创建:2014.03.09
/// </remarks>
/// </summary>
[ConfigurationCollection(typeof(KeyValueElement))]
public class KeyValueElementCollection : ConfigurationElementCollection
{
//忽略大小写
public KeyValueElementCollection() : base(StringComparer.OrdinalIgnoreCase) { }
/// <summary>
/// 集合中指定键的元素
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
new public KeyValueElement this[string name]
{
get { return (KeyValueElement)base.BaseGet(name); }
set
{
if (base.Properties.Contains(name)) base[name] = value;
else base.BaseAdd(value);
}
}
/// <summary>
/// 创建新元素.
/// 必须重写
/// </summary>
/// <returns></returns>
protected override ConfigurationElement CreateNewElement()
{
return new KeyValueElement();
}
/// <summary>
/// 获取元素的键
/// </summary>
/// <param name="element"></param>
/// <returns></returns>
protected override object GetElementKey(ConfigurationElement element)
{
return ((KeyValueElement)element).Key;
}
}
}
(2)类KeyValueElement:
using System.Configuration;
namespace VideoMonitorPlatform.Models.Config
{
/// <summary>
/// 键值元素类
/// <remarks>
/// 创建:2014.03.09
/// </remarks>
/// </summary>
public class KeyValueElement : ConfigurationElement
{
/// <summary>
/// 键
/// </summary>
[ConfigurationProperty("key", IsRequired = true)]
public string Key
{
get { return this["key"].ToString(); }
set { this["key"] = value; }
}
/// <summary>
/// 值
/// </summary>
[ConfigurationProperty("value")]
public string Value
{
get { return this["value"].ToString(); }
set { this["value"] = value; }
}
}
}
(3)类CustomerConfig:
using System.Configuration;
namespace VideoMonitorPlatform.Models.Config
{
/// <summary>
/// 上传设置配置节
/// <remarks>
/// 创建:2014.03.09
/// </remarks>
/// </summary>
public class CustomerConfig : ConfigurationSection
{
private static ConfigurationProperty _property = new ConfigurationProperty(string.Empty, typeof(KeyValueElementCollection), null, ConfigurationPropertyOptions.IsDefaultCollection);
/// <summary>
/// 配置列表
/// </summary>
[ConfigurationProperty("", Options = ConfigurationPropertyOptions.IsDefaultCollection)]
private KeyValueElementCollection KeyValues
{
get { return (KeyValueElementCollection)base[_property]; }
set { base[_property] = value; }
}
/// <summary>
/// NVR的IP
/// </summary>
public string NvrIp
{
get
{
if (KeyValues["NvrIp"] == null) return string.Empty;
return KeyValues["NvrIp"].Value;
}
set
{
if (KeyValues["NvrIp"] == null) KeyValues["NvrIp"] = new KeyValueElement() { Key = "NvrIp", Value = value };
else KeyValues["NvrIp"].Value = value;
}
}
/// <summary>
/// NVR的端口号
/// </summary>
public string NvrPort
{
get
{
if (KeyValues["NvrPort"] == null) return string.Empty;
return KeyValues["NvrPort"].Value;
}
set
{
if (KeyValues["NvrPort"] == null) KeyValues["NvrPort"] = new KeyValueElement() { Key = "NvrPort", Value = value };
else KeyValues["NvrPort"].Value = value;
}
}
/// <summary>
/// NVR的登录用户名
/// </summary>
public string NvrUserName
{
get
{
if (KeyValues["NvrUserName"] == null) return string.Empty;
return KeyValues["NvrUserName"].Value;
}
set
{
if (KeyValues["NvrUserName"] == null) KeyValues["NvrUserName"] = new KeyValueElement() { Key = "NvrUserName", Value = value };
else KeyValues["NvrUserName"].Value = value;
}
}
/// <summary>
/// NVR的登录密码
/// </summary>
public string NvrPassword
{
get
{
if (KeyValues["NvrPassword"] == null) return string.Empty;
return KeyValues["NvrPassword"].Value;
}
set
{
if (KeyValues["NvrPassword"] == null) KeyValues["NvrPassword"] = new KeyValueElement() { Key = "NvrPassword", Value = value };
else KeyValues["NvrPassword"].Value = value;
}
}
/// <summary>
/// 截图图片文件保存位置
/// </summary>
public string CaptureSavePath
{
get
{
if (KeyValues["CaptureSavePath"] == null) return string.Empty;
return KeyValues["CaptureSavePath"].Value;
}
set
{
if (KeyValues["CaptureSavePath"] == null) KeyValues["CaptureSavePath"] = new KeyValueElement() { Key = "CaptureSavePath", Value = value };
else KeyValues["CaptureSavePath"].Value = value;
}
}
}
}
4、调用代码
读取配置文件的代码如下:
var customerConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~")
.GetSection("CustomerConfig") as VideoMonitorPlatform.Models.Config.CustomerConfig;
string captureSavePath = customerConfig.CaptureSavePath;