C#操作配置文件(一)

.net下的配置文件分两种一种那个是应用程序配置文件,一种是web程序配置文件。C#操作配置文件时,通过ConfigurationManager来管理配置文件。

查询配置文件

利用ConfigurationManager.AppSettions根据key获取相应的value

string value=ConfigurationManager.AppSettings["test"];  //test为key值

更新、添加配置文件

        public static bool SetConfig(string key, string value)
        {
            try
            {
                Configuration conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

                if (!conf.AppSettings.Settings.AllKeys.Contains(key))  //查询配置文件中是否存在此key
                {
                    conf.AppSettings.Settings.Add(key, value); //添加
                }
                else
                {
                    conf.AppSettings.Settings[key].Value = value; //更新
                    conf.Save();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

总结:

对配置文件的操作还可以利用XmlDocument进行操作。下一次会总结。

时间: 2024-08-02 04:24:25

C#操作配置文件(一)的相关文章

C#操作配置文件(二)

上一篇博客介绍了利用ConfigurationManager去操作配置文件,这次总结一下,利用操作xml文件的方法去操作配置文件.利用 更新配置信息 /// <summary> /// 更新配置文件信息 /// </summary> /// <param name="name">配置文件字段名称</param> /// <param name="Xvalue">值</param> private

操作配置文件Properties

操作配置文件Properties Table of Contents 1 定义 2 读取配置值 3 修改和保存配置 4 注意 1 定义 csharp中在Settings.settings文件中定义配置字段,把作用范围定义为User,则运行时可更 改,Applicatiion则运行时不可更改,可以使用数据网格视图,很方便. 2 读取配置值 // FieldName是你定义的字段 text1.text = Properties.Settings.Default.FieldName; 3 修改和保存配

ASP.NET 操作配置文件

1.配置文件的各种操作 http://www.cnblogs.com/shimeng3344518/archive/2007/04/23/723999.html 2. http://www.jb51.net/article/29196.htm

ThiinkPhP 3.2 控制器操作 配置文件 语法(判断,循环) 连接数据库

控制器操作 空操作 系统在找不到请求的方法的时候,会定位到__empty()方法处理,利用这个机制,我们可以对用户请求的不存在的所有操作进行统一处理. 空控制器 当系统请求找不到控制器,会定位到空控制器上EmptyController. 跨控制器调用 1.直接实例化 2.A() A()用来实例化其他控制器的. $obj->display("Manager/ManagerTest1") //调用的是模版 3.R() R()和A()基本一样,唯一不同的是R()函数在实例化控制器的时候

Python操作配置文件configparser模块

在实际的开发过程中,我们常有操作ini格式和conf格式配置文件的操作,Python为我们提供了configparser模块,方便我们对配置文件进行读写操作. config.ini配置文件内容如下: [email] user = root password = aaaaaa port = 25 host = smtp.126.com [thread] thread = 3 1.读取配置文件 方法 说明 read(filename) 直接读取配置文件内容 sections() 以列表的形式返回所有

springboot整合mybatis及封装curd操作-配置文件

1 配置文件  application.properties  #server server.port=8090 server.address=127.0.0.1 server.session.timeout=1800 server.error.whitelabel.enabled=true #mybatis mybatis.config-locations=classpath:mybatis/mybatis-config.xml // mybatis配置文件 mybatis.mapper-lo

python模块ConfigParser操作配置文件

python模块ConfigParser 操作ini格式文件 cat test.txt [host] web01 = 10.10.10.10 web02 = 20.20.20.20 [db] mysql01 = 1.1.1.1 mysql02 = 2.2.2.2 #!/usr/bin/env python #coding:utf8 import ConfigParser,string,os,sys cf = ConfigParser.ConfigParser() cf.read("test.tx

java操作配置文件.properties

一会asp.net,一会java的..... Properties reqProperties = new Properties(); String path = System.getProperty("user.dir"); InputStream in = new FileInputStream(path+File.separator+"message.properties"); reqProperties.load(in); String attrType =

configparser操作配置文件

import configparser conf = configparser.ConfigParser()file_path = "E:\optimize_allchips\config\local.ini"print(conf.read(file_path, encoding='utf8'))print(conf.sections()) # 读取配置文件截点print(conf.options('RegisterElement')) # 读取截点下的内容print(conf.ite