Link to home
Start Free TrialLog in
Avatar of Andy Green
Andy GreenFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Save web settings to config file

I have the following VB code that works fine, I need this ti be conveeted to C#, but it seems C' does things differently.


        If chkGoingFlying.Checked Then
            Config.AppSettings.Settings.Item("GoingFlying").Value = 1
        Else
            Config.AppSettings.Settings.Item("GoingFlying").Value = 0
        End If

        Config.Save()


This is a web application and I have a reference at the top  - Private Config As Configuration.


From searching I'm confused whether to use CongigurationManager or WebConfigurationManager. Neither works.


Is anyone able to help.


Andy

Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

From searching I'm confused whether to use ConfigurationManager or WebConfigurationManager. Neither works.
Using WebConfigurationManager is the preferred way to work with configuration files related to Web applications. For client applications, use the ConfigurationManager class.

Share your converted c# code if you need any further assistance
Avatar of Andy Green

ASKER

My code - the error is that there is no definition for 'Save'

      protected void onGoingFlying_CheckedChanged(object sender, EventArgs e)
      {
         if (goingFlying.Checked)
         {
            ConfigurationManager.AppSettings["GoingFlying"] = "True";
         }
         else
         {
            ConfigurationManager.AppSettings["GoingFlying"] = "False";
         }
         ConfigurationManager.Save();
      }

I am not 100% sure, but it looks like you need to use

 configFile.Save(ConfigurationSaveMode.Modified);  

Open in new window



https://docs.microsoft.com/en-us/dotnet/api/system.configuration.configurationmanager.appsettings?view=dotnet-plat-ext-6.0

The full example from that article is
static void AddUpdateAppSettings(string key, string value)  
        {  
            try  
            {  
                var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);  
                var settings = configFile.AppSettings.Settings;  
                if (settings[key] == null)  
                {  
                    settings.Add(key, value);  
                }  
                else  
                {  
                    settings[key].Value = value;  
                }  
                configFile.Save(ConfigurationSaveMode.Modified);  
                ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);  
            }  
            catch (ConfigurationErrorsException)  
            {  
                Console.WriteLine("Error writing app settings");  
            }  
        }

Open in new window

This seems to pertain to console apps, I've tried changing to WebCOnfig (its a website) but cant get anything to work.

A
Actually got this working now. Here is my solution in case anyone else has a similar issue.

      private void UpdateAppSettings(string key, string value)
      {
         System.Configuration.Configuration configFile = null;
         configFile = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");

         var settings = configFile.AppSettings.Settings;
         settings[key].Value = value;

         configFile.Save(ConfigurationSaveMode.Modified);
         ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
      }

This is called passing in the Key and value:
UpdateAppSettings("GoingFlying", "True");

Andy
ASKER CERTIFIED SOLUTION
Avatar of Andy Green
Andy Green
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial