I'm trying to change in runtime one key of my applications settings file, but it does not work.
我试图在运行时更改我的应用程序设置文件的一个键,但它不起作用。
I do on that way:
我这样做:
ConfigurationSettings.AppSettings["XPTO"] = "HELLO";
It seems that it only changes in memory, not on the file.
它似乎只改变了内存,而不是文件。
Does anyone knows how to do this?
有谁知道怎么做?
Thanks.
3 个解决方案
#1
Take a look at my overview of .NET settings files...In short, I think you want a user-scoped setting. It will behave more like you expect.
看看我对.NET设置文件的概述......总之,我认为你想要一个用户范围的设置。它会表现得更像你期望的。
Edit: If you are using the settings designer in Visual Studio, then simply change the "Scope" to "User". If not, you should be able to do the equivalent programmatically.
编辑:如果您在Visual Studio中使用设置设计器,则只需将“范围”更改为“用户”。如果没有,您应该能够以编程方式执行等效操作。
#2
The AppSettings file is not designed to be writable. It is designed to store configurations that will not change at run time but might change over time ie: DB Connection Strings, web service URL's, etc.
AppSettings文件不是可写的。它旨在存储在运行时不会更改但可能随时间更改的配置,即:DB连接字符串,Web服务URL等。
So, while it may be possible to update the file in reality you should re-asses if this value should be stored there.
因此,虽然有可能实际更新文件,但您应该重新评估是否应将该值存储在那里。
#3
Assuming your app has write permissions on the file...
假设您的应用对该文件具有写权限...
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); // the config that applies to all users
AppSettingsSection appSettings = config.AppSettings;
if (appSettings.IsReadOnly() == false)
{
appSettings("Key").Value = "new value";
config.Save();
}
I'm ignoring all the possible exceptions that can be thrown...
我忽略了所有可能抛出的异常......
#1
Take a look at my overview of .NET settings files...In short, I think you want a user-scoped setting. It will behave more like you expect.
看看我对.NET设置文件的概述......总之,我认为你想要一个用户范围的设置。它会表现得更像你期望的。
Edit: If you are using the settings designer in Visual Studio, then simply change the "Scope" to "User". If not, you should be able to do the equivalent programmatically.
编辑:如果您在Visual Studio中使用设置设计器,则只需将“范围”更改为“用户”。如果没有,您应该能够以编程方式执行等效操作。
#2
The AppSettings file is not designed to be writable. It is designed to store configurations that will not change at run time but might change over time ie: DB Connection Strings, web service URL's, etc.
AppSettings文件不是可写的。它旨在存储在运行时不会更改但可能随时间更改的配置,即:DB连接字符串,Web服务URL等。
So, while it may be possible to update the file in reality you should re-asses if this value should be stored there.
因此,虽然有可能实际更新文件,但您应该重新评估是否应将该值存储在那里。
#3
Assuming your app has write permissions on the file...
假设您的应用对该文件具有写权限...
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); // the config that applies to all users
AppSettingsSection appSettings = config.AppSettings;
if (appSettings.IsReadOnly() == false)
{
appSettings("Key").Value = "new value";
config.Save();
}
I'm ignoring all the possible exceptions that can be thrown...
我忽略了所有可能抛出的异常......