博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
配置文件操作类
阅读量:7204 次
发布时间:2019-06-29

本文共 2852 字,大约阅读时间需要 9 分钟。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml;namespace ConsoleApplication1{    ///     /// 配置文件工具类    ///     public static class ConfigUtil    {        ///         /// 修改Config配置文件的值        ///         ///         ///         ///         public static void SetAppSettingsExe(string filePath, string key, string newValue)        {            try            {                var doc = new XmlDocument();                doc.Load(filePath);                doc.SelectSingleNode("/configuration/appSettings/add[@key='" + key + "']").Attributes["value"].Value = newValue;                doc.Save(filePath);                doc.Load(filePath);                doc = null;            }            catch (Exception ex)            {                throw ex;            }        }        ///         /// 获取Config配置文件的值        ///         ///         ///         public static string GetAppSettingsExe(string filePath, string key)        {            try            {                var doc = new XmlDocument();                doc.Load(filePath);                return doc.SelectSingleNode("/configuration/appSettings/add[@key='" + key + "']").Attributes["value"].Value;            }            catch (Exception ex)            {                throw ex;            }        }        ///         /// 修改vshost.exe.Config配置文件的值        ///         ///         ///         public static void SetAppSettingsVshostExe(string key, string value)        {            try            {                //vshost.exe.Config                var doc = new XmlDocument();                doc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);                doc.SelectSingleNode("/configuration/appSettings/add[@key='" + key + "']").Attributes["value"].Value = value;                doc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);            }            catch (Exception ex)            {                throw ex;            }        }        ///         /// 获取vshost.exe.Config配置文件的值        ///         ///         /// 
public static string GetAppSettingsVshostExe(string key) { try { var doc = new XmlDocument(); doc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); return doc.SelectSingleNode("/configuration/appSettings/add[@key='" + key + "']").Attributes["value"].Value; } catch (Exception ex) { throw ex; } } }}

 

 

调用:

ConfigUtil.SetAppSettingsVshostExe("aa123", "你们");            var v = ConfigUtil.GetAppSettingsExe(@"E:\项目\ConsoleApplication1\app.config", "aa");            var v2 = ConfigUtil.GetAppSettingsVshostExe("aa");            ConfigUtil.SetAppSettingsExe(@"E:\项目\ConsoleApplication1\app.config", "aa", "马");

转载地址:http://pfpum.baihongyu.com/

你可能感兴趣的文章
Globus的简单介绍
查看>>
[LeetCode] Search Insert Position 解题报告
查看>>
c# 的传递参数值传递与传递引用的区别,ref与out区别
查看>>
win7+vs2008+cuda5.x 环境配置二
查看>>
PHP5.5安装PHPRedis扩展
查看>>
c#Socket Tcp服务端编程
查看>>
java构造函数注意点
查看>>
Asp.net 中配置 CKEditor和CKFinder
查看>>
Use dynamic type in Entity Framework 4.1 SqlQuery() method
查看>>
《Python CookBook2》 第四章 Python技巧 - 若列表中某元素存在则返回之 && 在无须共享引用的条件下创建列表的列表...
查看>>
redhat网卡设置
查看>>
javascript 的作用域
查看>>
JFinal极速开发框架使用笔记(二) 两个问题,一个发现
查看>>
AutoCompleteTextView
查看>>
SecureCRT生成序列
查看>>
Android 应用程序主框架搭建
查看>>
2012腾讯春季实习生面试经历(二)
查看>>
用Bootstrap框架弹出iframe页面 在弹出的模态框中载人iframe页面,bootstrapiframe
查看>>
2012腾讯暑期实习面经(技术类web前端)
查看>>
第3种方法获取redis cluster主从关系
查看>>