问题描述
在我的VB.net程序中,不是C或C#我读了我的Config文件,例如:
In my program in VB.net, NOT C or C # I read my Config file for example like this:
Imports System.Configuration
Imports System.Collections.Specialized
Imports System.Collections.Generic
Module Module1
Sub LeggiConfig()
Dim sAttr As String
sAttr = ConfigurationSettings.AppSettings("Key1") ' li legge ad uno ad uno
Console.WriteLine("The value of Key1: " & sAttr)
end sub
End Module
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<appSettings>
<add key="Key0" value="0" />
<add key="Key1" value="1" />
<add key="Key2" value="2" />
</appSettings>
</configuration>
我想更改Key1参数,我尝试了以下方法:
I would like to change that Key1 parameter and I tried these ways:
ConfigurationSettings.AppSettings.Set(("Key1"), "pippo" & " ")
My.Settings.Save()
sAttr = ConfigurationSettings.AppSettings("Key1") ' li legge ad uno ad uno
Console.WriteLine("The value of Key1: " & sAttr)
如果我在控制台中这样写,我看到值pippo,但在配置文件中它仍然是初始值1
如果相反我添加这一行:
if I write so in the console I see the value pippo, but in the config file it still remains the initial value that was 1
if instead I add this line:
' ConfigurationSettings.AppSettings.Add(("Key1"), "pippo" & " ")
我有这个错误:
System.Configuration.ConfigurationErrorsException:'只读配置。'
这是如何解决的?
我尝试过的事情:
你能帮忙吗?我?
因为我试过我在上面写的
I have this error:
System.Configuration.ConfigurationErrorsException: 'Read-only configuration.'
how does this resolve?
What I have tried:
can you help Me ?
as I tried I wrote it above
推荐答案
Configuration configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
KeyValueConfigurationCollection settings = configFile.AppSettings.Settings;
if (settings["Key1"] == null)
{
settings.Add("Key1", "pippo");
}
else
{
settings["Key1"].Value = "pippo";
}
configFile.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
[]
这篇关于编写文件配置时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!