zoukankan      html  css  js  c++  java
  • 配置文件操作类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    
    namespace ConsoleApplication1
    {
        /// <summary>
        /// 配置文件工具类
        /// </summary>
        public static class ConfigUtil
        {
            /// <summary>
            /// 修改Config配置文件的值
            /// </summary>
            /// <param name="filePath"></param>
            /// <param name="key"></param>
            /// <param name="newValue"></param>
            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;
                }
            }
    
            /// <summary>
            /// 获取Config配置文件的值
            /// </summary>
            /// <param name="filePath"></param>
            /// <param name="key"></param>
            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;
                }
            }
    
            /// <summary>
            /// 修改vshost.exe.Config配置文件的值
            /// </summary>
            /// <param name="key"></param>
            /// <param name="value"></param>
            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;
                }
            }
    
            /// <summary>
            /// 获取vshost.exe.Config配置文件的值
            /// </summary>
            /// <param name="key"></param>
            /// <returns></returns>
            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", "马");
  • 相关阅读:
    mongodb3.6 (四)net 客户端如何连接、访问mongodb集群
    mongodb3.6 副本集(三)mongodb 如何做数据备灾
    winform中如何使用确认对话框
    Centos6.5在线配置安装Java环境与Tomcat环境
    IBatis.Net 下使用SqlBulkCopy 大批量导入数据 问题解决
    【easyui-combobox】下拉菜单自动补全功能,Ajax获取远程数据源
    IDEA创建springboot异常(Failed to load class "org.slf4j.impl.StaticLoggerBinder")
    Elasticsearch6.5安装&&常见问题与答案解释
    JS实现多Div模块拖拽功能
    IView入门练习~CDN模式全局加载JS
  • 原文地址:https://www.cnblogs.com/gossip/p/2514730.html
Copyright © 2011-2022 走看看