c#获取或修改配置文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
namespace SZTMinorTool
{
/// <summary>
/// App.config配置类
/// </summary>
public class AppSettings
{
/// <summary>
/// 获取配置文件路径
/// </summary>
/// <returns></returns>
public static string AppConfig()
{
return System.IO.Path.Combine(Application.StartupPath, "App.config");//此处配置文件在程序目录下,或者设置为指定的配置文件路径
}
/// <summary>
/// 获取配置节点值
/// </summary>
/// <param name="appKey">节点key值</param>
/// <returns></returns>
public static string GetValue(string appKey)
{
XmlDocument xDoc = new XmlDocument();
try
{
xDoc.Load(AppSettings.AppConfig());
XmlNode xNode;
XmlElement xElem;
xNode = xDoc.SelectSingleNode("//appSettings"); //补充,需要在你的app.config 文件中增加一下,<appSetting> </appSetting>
xElem = (XmlElement)xNode.SelectSingleNode("//add[@key='" + appKey + "']");
if (xElem != null)
return xElem.GetAttribute("value");
else
return "";
}
catch (Exception)
{
return "";
}
}
/// <summary>
/// 设置配置节点值
/// </summary>
/// <param name="AppKey">key</param>
/// <param name="AppValue">value</param>
public static void SetValue(string AppKey, string AppValue)
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(AppSettings.AppConfig());
XmlNode xNode;
XmlElement xElem1;
XmlElement xElem2;
xNode = xDoc.SelectSingleNode("//appSettings");
xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
if (xElem1 != null)
{
xElem1.SetAttribute("value", AppValue);
}
else
{
xElem2 = xDoc.CreateElement("add");
xElem2.SetAttribute("key", AppKey);
xElem2.SetAttribute("value", AppValue);
xNode.AppendChild(xElem2);
}
xDoc.Save(AppSettings.AppConfig());
}
}
}
c#获取或修改配置文件的更多相关文章
- 在Openfire中使用自己的数据表之修改配置文件
目前我使用的Openfire版本是3.10.3,以下使用说明也是在这个版本上做的修改. Openfire提供了两种方式使用用户数据表.一种是安装完成之后默认实现的org.jivesoftware.op ...
- SpringBoot配置分析、获取到SpringBoot配置文件信息以及几种获取配置文件信息的方式
Spring入门篇:https://www.cnblogs.com/biehongli/p/10170241.html SpringBoot的默认的配置文件application.properties ...
- web项目部署在不同环境中需要修改配置文件的解决方法
web项目部署中存在的配置文件问题: web项目以war包的形式,部署在tomcat中,同时项目需要访问一些其他的东东,例如访问数据库,调用别的项目的API.在开发中,这些需要访问的外部地址通常以配置 ...
- redis 命令行查看修改配置文件项、配置文件说明
命令行查看修改配置文件项 config get | config set | config rewrite config get requirepass // 获取密码config set requi ...
- centos lamp/lnmp阶段复习 以后搬迁discuz论坛不需要重新安装,只需修改配置文件即可 安装wordpress 安装phpmyadmin 定时备份mysql两种方法 第二十五节课
centos lamp/lnmp阶段复习 以后搬迁discuz论坛不需要重新安装,只需修改配置文件即可 安装wordpress 安装phpmyadmin 定时备份mysql两种方法 第二十五节 ...
- configparser模块来生成和修改配置文件
1. 安装configparser模块 pip3 install configparser ##python2.7模块名为ConfigParser 2. 创建配置文件 import configpar ...
- python--如何在线上环境优雅的修改配置文件?
1.如何在线上环境优雅的修改配置文件? 原配置文件 #原配置文件 global log 127.0.0.1 local2 daemon maxconn 256 log 127.0.0.1 local2 ...
- 手动修改ARM Linux的静态IP和用QT获取和修改IP
.vi /etc/network/interfaces # 找到“# Wired or wireless interfaces” .注释掉 iface eth0 inet dhcp 这是自动获取ip地 ...
- Ubuntu通过修改配置文件进行网络配置
Ubuntu系统进行网络配置有的时候用图形界面不起作用,这种情况下可以直接修改某些启动脚本或配置文件 Ubuntu系统进行网络配置涉及到几个配置文件1./etc/network/interfaces ...
随机推荐
- ArcEngine 通过SpatialRelDescription删除不相交要素
ISpatialFilter.SpatialRel设置为esriSpatialRelRelate,并且设置SpatialRelDescription为某个字符串.该字符串的构造方法:该字符串为长度为9 ...
- GitHub的代码托管和使用方法
原文 GitHub托管 借助GitHub托管你的项目代码 PS:话说自己注册了GitHub都很久了,却没有怎么去弄,现在系统学习一下,也把自己的学习经历总结下来share给大家,希望大家都能把Git ...
- 点击图片名,让图片在pictureBox中显示 z
public string filepath; public Form1() { InitializeComponent(); } private void button1_Click(object ...
- android 状态栏、标题栏、屏幕高度
1.获取状态栏高度: decorView是window中的最顶层view,可以从window中获取到decorView,然后decorView有个getWindowVisibleDisplayFram ...
- 11、NFC技术:NDEF Uri格式解析
NDEF Uri格式规范 与NDEF文本格式一样,存储在NFC标签中的Uri也有一定的格式 http://www.nfc-forum.org/specs/spec_dashboard 编写可以解析Ur ...
- delphi 中字符串与16进制、10进制转换函数
//字符串转成16进制代码function strToHexStr(str:string):string;varc:char;ss:string;i:integer;beginwhile str& ...
- ansible条件使用--实践
ansible条件使用 1.条件使用最简单的方式 ansible中使用条件最简单的方式如下所示: [root@ansibleserver kel]# cat conditions.yml --- - ...
- 【LeetCode】12 & 13 - Integer to Roman & Roman to Integer
12 - Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be wit ...
- 积累PDU
PDU,即专业发展单元PDUs(Professional Development Units)用来量化PMP所参与的学习和专业服务活动.典型情况下,每个小时所进行的有计划的.系统的学习或专业活动可获得 ...
- C++11 之 " = delete "
1 缺省函数 设计一个类,没有成员函数 (member function),只有成员数据 (member data) class DataOnly { private: std::string st ...