.net中app.config的示例

本文介绍如何将软件配置放入app.config文件中,包括创建配置文件、示例代码、引入动态库及通过代码读取配置值的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以后一些技术性的问题,总结在此。


这几天想把一些软件中的配置放到app.config中,这种做法应该是很成熟的,但是在实际使用中还是遇到了一些问题。

总结一下:

1、添加配置文件:添加--新建项--应用程序配置文件。这个时候就能看到app.config文件了。


2、示例:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>


  <configSections>
    <section name="serverConfig" type="System.Configuration.NameValueSectionHandler"/>
    <sectionGroup name="pss">
      <section name="waveLength" type="System.Configuration.NameValueSectionHandler"/>  
    </sectionGroup>
    
  </configSections>
  
  <appSettings>
    <add key ="LD" value ="DFB;DFB TOSA;FP"/>
  </appSettings>

  <serverConfig>
    <add key ="serverName" value ="genuine"/>
    <add key ="dtName" value ="device"/>
    <add key ="newdtName" value ="Lte"/>
  </serverConfig>
  
  <pss>
    <waveLength>
      <add key ="850" value ="0"/>   
      <add key ="1310" value ="1"/>
    </waveLength>    
  </pss>

</configuration>


其中<configSections>必须放在<appSettings>前面~!
并且在section 中需要增加type="System.Configuration.NameValueSectionHandler"。


3、引入动态库以及命名空间:
添加引用--System.Configuration
Imports System.Configuration


4、代码中获取这里面的值的代码:
Dim s As Specialized.NameValueCollection = ConfigurationManager.GetSection("serverConfig")
Dim e As String = serverConfig("serverName")
Dim s As Specialized.NameValueCollection = ConfigurationManager.GetSection("pss/waveLength")
Dim e As String = serverConfig("850")


Dim f As String = ConfigurationManager.AppSettings("LD")


这样一些基本配置都可以实现了。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值