using System.Xml;
using System.IO;
using System; namespace Framework.Common
{
/// <summary>
/// 用于获取或设置Web.config/*.exe.config中节点数据的辅助类
/// </summary>
public sealed class AppConfig
{
private string filePath; /// <summary>
/// 从当前目录中按顺序检索Web.Config和*.App.Config文件。
/// 如果找到一个,则使用它作为配置文件;否则会抛出一个ArgumentNullException异常。
/// </summary>
public AppConfig()
{
string webconfig = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Web.Config");
string appConfig = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile.Replace(".vshost", ""); if (File.Exists(webconfig))
{
filePath = webconfig;
}
else if (File.Exists(appConfig))
{
filePath = appConfig;
}
else
{
throw new ArgumentNullException("没有找到Web.Config文件或者应用程序配置文件, 请指定配置文件");
}
} /// <summary>
/// 用户指定具体的配置文件路径
/// </summary>
/// <param name="configFilePath">配置文件路径(绝对路径)
public AppConfig(string configFilePath)
{
filePath = configFilePath;
} /// <summary>
/// 设置程序的config文件
/// </summary>
/// <param name="keyName">键名
/// <param name="keyValue">键值
public void AppConfigSet(string keyName, string keyValue)
{
//由于存在多个Add键值,使得访问appSetting的操作不成功,故注释下面语句,改用新的方式
/*
string xpath = "//add[@key='" + keyName + "']";
XmlDocument document = new XmlDocument();
document.Load(filePath); XmlNode node = document.SelectSingleNode(xpath);
node.Attributes["value"].Value = keyValue;
document.Save(filePath);
*/ XmlDocument document = new XmlDocument();
document.Load(filePath); XmlNodeList nodes = document.GetElementsByTagName("add");
for (int i = 0; i < nodes.Count; i++)
{
//获得将当前元素的key属性
XmlAttribute attribute = nodes[i].Attributes["key"];
//根据元素的第一个属性来判断当前的元素是不是目标元素
if (attribute != null && (attribute.Value == keyName))
{
attribute = nodes[i].Attributes["value"];
//对目标元素中的第二个属性赋值
if (attribute != null)
{
attribute.Value = keyValue;
break;
}
}
}
document.Save(filePath);
} /// <summary>
/// 读取程序的config文件的键值。
/// 如果键名不存在,返回空
/// </summary>
/// <param name="keyName">键名
/// <returns></returns>
public string AppConfigGet(string keyName)
{
string strReturn = string.Empty;
try
{
XmlDocument document = new XmlDocument();
document.Load(filePath); XmlNodeList nodes = document.GetElementsByTagName("add");
for (int i = 0; i < nodes.Count; i++)
{
//获得将当前元素的key属性
XmlAttribute attribute = nodes[i].Attributes["key"];
//根据元素的第一个属性来判断当前的元素是不是目标元素
if (attribute != null && (attribute.Value == keyName))
{
attribute = nodes[i].Attributes["value"];
if (attribute != null)
{
strReturn = attribute.Value;
break;
}
}
}
}
catch
{
;
} return strReturn;
} /// <summary>
/// 获取指定键名中的子项的值
/// </summary>
/// <param name="keyName">键名
/// <param name="subKeyName">以分号(;)为分隔符的子项名称
/// <returns>对应子项名称的值(即是=号后面的值)</returns>
public string GetSubValue(string keyName, string subKeyName)
{
string connectionString = AppConfigGet(keyName).ToLower();
string[] item = connectionString.Split(new char[] { ';' }); for (int i = 0; i < item.Length; i++)
{
string itemValue = item[i].ToLower();
if (itemValue.IndexOf(subKeyName.ToLower()) >= 0) //如果含有指定的关键字
{
int startIndex = item[i].IndexOf("="); //等号开始的位置
return item[i].Substring(startIndex + 1); //获取等号后面的值即为Value
}
}
return string.Empty;
} #region 一些常用的配置项属性 /// <summary>
/// 从配置文件获取权限系统链接(配置项HWSecurity的值)
/// </summary>
public string HWSecurity
{
get
{
return AppConfigGet("HWSecurity");
}
} /// <summary>
/// 系统的标识ID(配置项System_ID的值)
/// </summary>
public string System_ID
{
get
{
return AppConfigGet("System_ID");
}
} /// <summary>
/// 应用程序名称(配置项ApplicationName的值)
/// </summary>
public string AppName
{
get
{
return AppConfigGet("ApplicationName");
}
} /// <summary>
/// 软件厂商名称(配置项Manufacturer的值)
/// </summary>
public string Manufacturer
{
get
{
return AppConfigGet("Manufacturer");
}
} /// <summary>
/// 设置程序的config文件的Enterprise Library的数据库链接地址
/// </summary>
/// <param name="keyName">键名
/// <param name="keyValue">键值
public void SetConnectionString(string keyName, string keyValue)
{
XmlDocument document = new XmlDocument();
document.Load(filePath); XmlNodeList nodes = document.GetElementsByTagName("add");
for (int i = 0; i < nodes.Count; i++)
{
//获得将当前元素的name属性
XmlAttribute att = nodes[i].Attributes["name"];
//根据元素的第一个属性来判断当前的元素是不是目标元素
if (att != null && (att.Value == keyName))
{
att = nodes[i].Attributes["connectionString"];
if (att != null)
{
att.Value = keyValue;
break;
}
}
}
document.Save(filePath);
} /// <summary>
/// 读取程序的config文件Enterprise Library的数据库链接地址
/// </summary>
/// <param name="keyName">键名
/// <returns></returns>
public string GetConnectionString(string keyName)
{
string strReturn = string.Empty;
try
{
XmlDocument document = new XmlDocument();
document.Load(filePath); XmlNodeList nodes = document.GetElementsByTagName("add");
for (int i = 0; i < nodes.Count; i++)
{
//获得将当前元素的key属性
XmlAttribute att = nodes[i].Attributes["name"];
//根据元素的第一个属性来判断当前的元素是不是目标元素
if (att != null && (att.Value == keyName))
{
att = nodes[i].Attributes["connectionString"];
if (att != null)
{
strReturn = att.Value;
break;
}
}
}
}
catch
{ ; } return strReturn;
} /// <summary>
/// 获取数据库配置信息
/// </summary>
/// <param name="keyName">节点名称
/// <returns></returns>
public DatabaseInfo GetDatabaseInfo(string keyName)
{
string connectionString = GetConnectionString(keyName);
return new DatabaseInfo(connectionString);
} /// <summary>
/// 设置数据库配置信息
/// </summary>
/// <param name="keyName">
/// <param name="databaseInfo">
public void SetDatabaseInfo(string keyName, DatabaseInfo databaseInfo)
{
SetConnectionString(keyName, databaseInfo.ConnectionString);
} #endregion
} }

读取配置文件-AppConfig的更多相关文章

  1. 【无私分享:ASP.NET CORE 项目实战(第八章)】读取配置文件(二) 读取自定义配置文件

    目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 我们在 读取配置文件(一) appsettings.json 中介绍了,如何读取appsettings.json. 但随之产生 ...

  2. ASP.NET Core实现类库项目读取配置文件

    前言 之前继续在学习多线程方面的知识,忽然这两天看到博问中有个园友问到如何在.net core类库中读取配置文件,当时一下蒙了,这个提的多好,我居然不知道,于是这两天了解了相关内容才有此篇博客的出现, ...

  3. Asp.NetCore 读取配置文件帮助类

    /// <summary> /// 读取配置文件信息 /// </summary> public class ConfigExtensions { public static ...

  4. Asp.net Core中使用Redis 来保存Session, 读取配置文件

    今天 无意看到Asp.net Core中使用Session ,首先要使用Session就必须添加Microsoft.AspNetCore.Session包,默认Session是只能存去字节,所以如果你 ...

  5. Asp.net Core 和类库读取配置文件信息

    Asp.net Core 和类库读取配置文件信息 看干货请移步至.net core 读取配置文件公共类 首先开一个脑洞,Asp.net core 被使用这么长时间了,但是关于配置文件(json)的读取 ...

  6. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件

    精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件 内容简介:本文介绍 Spring Boot 的配置文件和配置管理,以及介绍了三种读取配置文 ...

  7. 解决IntelliJ IDEA无法读取配置文件的问题

    解决IntelliJ IDEA无法读取配置文件的问题 最近在学Mybatis,按照视频的讲解在项目的某个包里建立配置文件,然后读取配置文件,但是一直提示异常. 读取配置文件的为官方代码: String ...

  8. java-工具类-读取配置文件

    java读取配置文件,当发现文件被修改后则重新加载 package com.zg.config; import java.io.File; import java.io.FileInputStream ...

  9. java 4种方式读取配置文件 + 修改配置文件

    版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[-] 方式一采用ServletContext读取读取配置文件的realpath然后通过文件流读取出来 方式二采用ResourceB ...

随机推荐

  1. Mysql数据库一:安装与创建windows服务

    Mysql数据库安装与创建windows服务 1.先下载压缩包(mysql-5.7.18-winx64.zip)移动到对应目录(如D:\software)后解压. 2.安装服务端: mysqld:带d ...

  2. 一,php的错误处理和异常处理

    php程序中如果语法或逻辑错误,会引起php默认错误处理机制,不会引起异常处理机制,只有在程序中throw抛出异常后,如果没有catch捕捉异常,默认调用php默认异常处理. php有默认错误机制和默 ...

  3. Ubuntu 16.04LTS安装Nginx

    Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Igor Sysoev ...

  4. 使用request.js代理post失败的问题

    前面写过一篇使用request.js做代理的文章,可能眼睛敏锐的朋友已经看出在代理POST方法时和代理其它请求方式是有区别的, 现在我来说一下为什么要这么处理. 相信很多人都采用这种方式去代理POST ...

  5. c++之sleep函数

    c++之sleep函数 c++中使用sleep函数需要导入第三方库,标准库中没有该函数实现. 我们导入window.h使用Sleep()方法,注意:第一个S要大写,括号中的表示的整数倍的毫秒 Slee ...

  6. POJ 2390

    import java.util.*; public class Main { public static void main(String args[]){ double interest; Sca ...

  7. (转)CentOS 7 单用户模式+救援模式

    原文:http://blog.51cto.com/asd9577/1931442 https://www.cnblogs.com/zhangzeyu/p/6379754.html-------Cent ...

  8. (转)MySQL登陆后提示符的修改

    MySQL登陆后提示符的修改 方法一:mysql命令行修改方式 mysql>prompt \u@night \r:\m:\s-> PROMPT set to '\u@night \r:\m ...

  9. 学生信息管理系统-顺序表&&链表(数据结构第一次作业)

    实验目的 : 1 .掌握线性表的定义: 2 .掌握线性表的基本操作,如建立.查找.插入和删除等. 实验内容: 定义一个包含学生信息(学号,姓名,成绩)的的 顺序表和链表,使其具有如下功能: (1) 根 ...

  10. 2018春招-美团后台开发方向编程题 (python实现)

    第一题:字符串距离 题目: 给出两个相同长度的由字符 a 和 b 构成的字符串,定义它们的距离为对应位置不同的字符的数量.如串”aab”与串”aba”的距离为 2:串”ba”与串”aa”的距离为 1: ...