以后一些技术性的问题,总结在此。
这几天想把一些软件中的配置放到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")
这样一些基本配置都可以实现了。