-
June 16th, 2021, 11:09 PM
#1
How do I update the Keys in the app.config file at runtime ?
I found some code examples that update the keys in the app.config file when running without error and not updating, why the code does not update, see my code
file app.config
Code:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="FormatImage" value="Jpg" />
<add key="LogFile" value="C:\Windows\Log" />
</appSettings>
</configuration>
Code:
private void cmdUpdate_Click(object sender, EventArgs e)
{
EditAppSetting("LogFile", "D:\Office2010");
}
public static void EditAppSetting(string key, string value)
{
try
{
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings[key].Value = value;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}
catch (Exception ex)
{
MessageBox.Show("Error update file App.config: " + ex.Message, "Thông báo kiểm tra !", MessageBoxButtons.OK, MessageBoxIcon.Question);
}
}
-
June 17th, 2021, 01:12 AM
#2
Re: How do I update the Keys in the app.config file at runtime ?
Victor Nijegorodov
-
June 17th, 2021, 02:42 AM
#3
Re: How do I update the Keys in the app.config file at runtime ?
I have debugged and set error trap the program doesn't give error
-
June 17th, 2021, 03:58 AM
#4
Re: How do I update the Keys in the app.config file at runtime ?
Originally Posted by dongtrien
I have debugged and set error trap the program doesn't give error
You have to debug your cede step-by-step and check whether you edit/save the correct file, whether you set the correct values to correct variables and so on...
Victor Nijegorodov
-
June 17th, 2021, 11:34 AM
#5
Re: How do I update the Keys in the app.config file at runtime ?
Are you sure the file doesn't update? Keep in mind the app.config file is located in the project folder but gets copied (and maybe renamed) to the project output folder during the build. So you need to check the copy in the output folder for changes (and these changes will get overwritten each time you build).
A word of caution. App.Config, Web.Config, and the newer AppSettings.json files are all intended to be readonly and not written to. The reason for this is typically the .net framework is monitoring the file for changes and will recycle the app domain when it detects the file has changed.
For this reason your app should not write to *.config or appsettings.json files. If you need to save settings, put them in a seperate file.
-
June 21st, 2021, 08:33 PM
#6
Re: How do I update the Keys in the app.config file at runtime ?
When in code editor mode I run the program the *.config file is not updated, this file is read-only, when I compile to the *.exe executable, I run the *.exe executable. I see there are updates, edit keys but I check keys in the app.config file has no updates and edits, so when running the *.exe executable file, where are the keys in app.config saved ?
-
June 23rd, 2021, 10:39 AM
#7
Re: How do I update the Keys in the app.config file at runtime ?
Originally Posted by dongtrien
When in code editor mode I run the program the *.config file is not updated, this file is read-only, when I compile to the *.exe executable, I run the *.exe executable. I see there are updates, edit keys but I check keys in the app.config file has no updates and edits, so when running the *.exe executable file, where are the keys in app.config saved ?
I've answered this in post #5.
Are you sure the file doesn't update? Keep in mind the app.config file is located in the project folder but gets copied (and maybe renamed) to the project output folder during the build. So you need to check the copy in the output folder for changes (and these changes will get overwritten each time you build).
-
June 24th, 2021, 09:12 PM
#8
Re: How do I update the Keys in the app.config file at runtime ?
Thank you for your help, I understand it
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|