C#中怎么读取配置文件

一.文件格式:App.config

1、配置文件的概述

应用程序配置文件是标准的 XML 文件。配置文件的根节点是configuration。我们经常访问的是appSettings,它是由.Net预定义的配置节。下面的“配置节”可以理解为进行配置一个XML的节点。

常见配置文件模式:

<configuration>
<configSections>                  //配置节声明区域,包含配置节和命名空间声明
<section>                             //配置节声明
<sectionGroup>                  //定义配置节组
<section>                           //配置节组中的配置节声明
<appSettings>                  //预定义配置节
<Custom element for configuration section>   //配置节设置区域

下面是一个最常见的应用程序配置文件的例子,只有appSettings节:


<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <appSettings>
        <add key="connectionstring" value="User Source=.;Password=;Initial Catalog=test;Provider=SQLOLEDB.1;" />
        <add key="TemplatePATH" value="Template" />
    </appSettings>
</configuration> 

在预定义的 appSettings 节(注意大小写),有很多的元素,这些元素名称都是“add”,有两个属性分别是“key”和“value”。

2、配置文件的读写

(1)添加引用:System.configguration

(2)代码头部引入命名空间 using System.Configuration

(3)通过ConfigurationManager类获取相关字段

1. textBox1.Text = ConfigurationManager.AppSettings["Key"];

例:textBox1.Text = ConfigurationManager.AppSettings["connectionstring"];

二、文件格式:xxx.ini

INI配置文件的组成?

    INI文件是文本文件,由若干节(section)组成,在每个带中括号的节名称下,是若干个关键词(key)及其对应的值(Value),这些关键词(key)属于位于关键词(key)上的节(section)。
[Section]
Key1=Value1
Key2=Value2

这里为了使用方便,我们将读写INI配置文件的关键代码封装在一个类中

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
 
namespace ToolsLibrary
{
    public class IniFile
    {
        public string path;             //INI文件名 
         
        //声明写INI文件的API函数
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
         
        //声明读INI文件的API函数
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
 
        //类的构造函数,传递INI文件的路径和文件名
        public IniFile(string INIPath) 
        { 
            path = INIPath; 
        } 
 
        //写INI文件
        public void IniWriteValue(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, path);
        }
 
        //读取INI文件
        public string IniReadValue(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, path);
            return temp.ToString();
        }
    } 
}

在以后使用的时候,我们只要实例化一个IniFile对象,即可通过这个对象中的方法读写INI配置文件。
例如读取INI配置文件中的值

IniFile ini = new IniFile("D://xxx.ini"); 
BucketName = ini.IniReadValue("operatorinformation","bucket");
OperatorName = ini.IniReadValue("operatorinformation", "operatorname");
OperatorPwd = ini.IniReadValue("operatorinformation", "operatorpwd");

将值写入INI配置文件中

IniFile ini = new IniFile("D://config.ini"); 
ini.IniWriteValue("operatorinformation", "bucket", BucketName);
ini.IniWriteValue("operatorinformation", "operatorname", OperatorName);
ini.IniWriteValue("operatorinformation", "operatorpwd", OperatorPwd);

ini文件格式

[operatorinformation]
bucket=3000000

[operatorinformation]
operatorname=22

[operatorinformation]
operatorpwd=23

 

转自:https://www.cnblogs.com/s7ven/articles/3575309.html

 

  • 9
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值