CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2007
    Posts
    405

    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);
                }
            }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How do I update the Keys in the app.config file at runtime ?

    Did you debug your code?
    Victor Nijegorodov

  3. #3
    Join Date
    Sep 2007
    Posts
    405

    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

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How do I update the Keys in the app.config file at runtime ?

    Quote Originally Posted by dongtrien View Post
    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

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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.

  6. #6
    Join Date
    Sep 2007
    Posts
    405

    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 ?

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How do I update the Keys in the app.config file at runtime ?

    Quote Originally Posted by dongtrien View Post
    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).

  8. #8
    Join Date
    Sep 2007
    Posts
    405

    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
  •  





Click Here to Expand Forum to Full Width

Featured