保存到app.config appSettings值

时间:2015-02-04 10:22:45

标签: vb.net

我有以下app.config。我想保存到ConnectionString键值 - 我的序列化xml文本。

Private Shared strcon As String = New AppSettingsReader().GetValue("ConnectionString", GetType(System.String)).ToString()

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings>
           <add key="ConnectionString" value=""/>  
      </appSettings>
        <system.diagnostics>
            <sources>
...

到目前为止,我得到了这段代码,但我收到错误:“对象引用没有设置为对象的实例。”

就行:config.AppSettings.Settings(“ConnectionString”)。Value =“myconn”

' Get the configuration file.
        Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)

        ' Update the setting.
        config.AppSettings.Settings("ConnectionString").Value = "myconn" 

        ' Save the configuration file.
        config.Save(ConfigurationSaveMode.Modified)

        ' Force a reload of the changed section.
        ConfigurationManager.RefreshSection("appSettings")

0 个答案:

没有答案