Avatar of Andy Green
Andy Green
Flag 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

ASP.NETWeb ApplicationsWeb Development

Avatar of undefined
Last Comment
Andy Green

8/22/2022 - Mon
David H.H.Lee

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

Scott Fell

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

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Andy Green

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

A
Andy Green

ASKER
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
Andy Green

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.