代码之家  ›  专栏  ›  技术社区  ›  D T

如何在Net5中读取配置文件?

  •  0
  • D T  · 技术社区  · 5 年前

    我使用Vb.net,FW:Net5

    这是我的app.config:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
       <appSettings>
        <add key="ConnectString" value="DB/Sample.db3" />
      </appSettings>
        <system.diagnostics>
            <sources>
                <!-- This section defines the logging configuration for My.Application.Log -->
                <source name="DefaultSource" switchName="DefaultSwitch">
                    <listeners>
                        <add name="FileLog"/>
                        <!-- Uncomment the below section to write to the Application Event Log -->
                        <!--<add name="EventLog"/>-->
                    </listeners>
                </source>
            </sources>
            <switches>
                <add name="DefaultSwitch" value="Information" />
            </switches>
            <sharedListeners>
                <add name="FileLog"
                     type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" 
                     initializeData="FileLogWriter"/>
                <!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
                <!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
            </sharedListeners>
        </system.diagnostics>
    </configuration>
    

    "System.Configuration.ConfigurationManager" 来自Nuget

    我已经尝试将配置设置为:

    Dim str = System.Configuration.ConfigurationManager.AppSettings("ConnectString")
    

    但出现异常错误:

    Configuration system failed to initialize
    

    如何在vbnet5中读取app.config?

    1 回复  |  直到 5 年前
        1
  •  1
  •   jmcilhinney    5 年前

    <system.diagnostics> 元素,并按预期工作。我不知道你会怎么做,如果你真的需要使用该部分,但它看起来不像你这样做,应该不是一个问题,评论出来或删除它。