如何在控制台应用程序中将值永久写入 app.config 文件?
How to write value to app.config file permanently in a console application?
在控制台应用程序项目中,我试图将值写入 app.config 文件并永久保存,但只能将其保存在内存中。
我已经尝试了 Write values in app.config file 中的所有解决方案,但 none 对我有用。
有什么想法可以使更改永久生效吗?
app.config 文件:
<appSettings>
<add key="test" value="123456" />
</appSettings>
Class 有两种方法只将值保存到内存中:
public static class ConfigurationHelper
{
public static void SetSetting(string key, string value)
{
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
configuration.AppSettings.Settings[key].Value = value;
configuration.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}
public static void SetSetting2(string key, string value)
{
Configuration configuration = ConfigurationManager.
OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
configuration.AppSettings.Settings[key].Value = value;
configuration.Save();
ConfigurationManager.RefreshSection("appSettings");
}
}
写出更改的简单测试,但是当重建项目或在记事本中打开 app.config 时,更改未保存:
[Test]
public void FirstTest()
{
Console.WriteLine("Value before: " + ConfigurationHelper.Get<string>("test"));
ConfigurationHelper.SetSetting2("test", "NewValue");
Console.WriteLine("Value after: " + ConfigurationHelper.Get<string>("test"));
}
据我所知,来自 app.config
的信息在创建程序时被使用,但 app.config
键和值是只读的,但您不必使用 app.config
来在应用程序中存储值,尝试使用这个:
步骤 1
转到 solution explorer -> Properties -> Settings
并创建设置文件。
步骤 2
添加您要使用的设置
步骤 3
在代码中使用
添加using yourNameSpace.Properties;
读取设置值:
var name = Settings.Default.Name1;
设置的设置值:
Settings.Default.Name1 = value;
保存设置:
Settings.Default.Save();
希望对你有所帮助。
在控制台应用程序项目中,我试图将值写入 app.config 文件并永久保存,但只能将其保存在内存中。 我已经尝试了 Write values in app.config file 中的所有解决方案,但 none 对我有用。
有什么想法可以使更改永久生效吗?
app.config 文件:
<appSettings>
<add key="test" value="123456" />
</appSettings>
Class 有两种方法只将值保存到内存中:
public static class ConfigurationHelper
{
public static void SetSetting(string key, string value)
{
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
configuration.AppSettings.Settings[key].Value = value;
configuration.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}
public static void SetSetting2(string key, string value)
{
Configuration configuration = ConfigurationManager.
OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
configuration.AppSettings.Settings[key].Value = value;
configuration.Save();
ConfigurationManager.RefreshSection("appSettings");
}
}
写出更改的简单测试,但是当重建项目或在记事本中打开 app.config 时,更改未保存:
[Test]
public void FirstTest()
{
Console.WriteLine("Value before: " + ConfigurationHelper.Get<string>("test"));
ConfigurationHelper.SetSetting2("test", "NewValue");
Console.WriteLine("Value after: " + ConfigurationHelper.Get<string>("test"));
}
据我所知,来自 app.config
的信息在创建程序时被使用,但 app.config
键和值是只读的,但您不必使用 app.config
来在应用程序中存储值,尝试使用这个:
步骤 1
转到 solution explorer -> Properties -> Settings
并创建设置文件。
步骤 2
添加您要使用的设置
步骤 3
在代码中使用
添加using yourNameSpace.Properties;
读取设置值:
var name = Settings.Default.Name1;
设置的设置值:
Settings.Default.Name1 = value;
保存设置:
Settings.Default.Save();
希望对你有所帮助。