Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
please help me


How do I...save and edit or update and read connection string in the "application configuration file" or app.config
Posted

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();

//StartPath means path of app.config file generally presents in bin folder
fileMap.ExeConfigFilename = StartPath + \\YourApp.exe.config;

// Open config file
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

//For Reading the values from app.config file
string KeyValue = config.AppSettings.Settings["KeyName"].Value;

//For removing the key
config.AppSettings.Settings.Remove("KeyName");

//For Adding the Key
config.AppSettings.Settings.Add("KeyName", "Valuetopass"); 

//After any changes in Config file need to save as follows
config.Save(ConfigurationSaveMode.Full);
 
Share this answer
 
v2
Comments
NorouziFar 6-Oct-14 6:01am    
but my code in app.config is:
<pre lang="HTML"><configuration>
<connectionstrings>
<add name="Acc" connectionstring="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Gaz.accdb"></add>
</connectionstrings>
</configuration></pre>

it is different with your code, i think that's not work!!
To read a connection string I have a function that does that. I pass in the name of the connection and it returns the connection string.

C#
public string GetConnectionStringsValue(string key)
        {
            string configFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
            
            string result = string.Empty;
            if (!string.IsNullOrEmpty(key))
            {
                ConnectionStringSettings connSettings = ConfigurationManager.ConnectionStrings[key];
                if (connSettings != null)
                {
                    result = connSettings.ConnectionString;
                }
            }
            
            return result;
        }
 
Share this answer
 
Comments
NorouziFar 7-Oct-14 2:41am    
how update or edite ?
Hello

May be the following link is useful to you.


Configuring a Connection String in the App.Config File During Runtime in C#[^]
 
Share this answer
 
C#
public void UpdateConfigFile(string key, string value)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings.Remove(key);
            config.AppSettings.Settings.Add(key, value);
            config.Save(ConfigurationSaveMode.Minimal);
            ConfigurationManager.RefreshSection("appSettings");
        }


This code snippet updates a value in the config (by removing it and then adding it) and then refreshes the section that contains the value (in this case the assSettings section).
 
Share this answer
 
Comments
sp_suresh 7-Oct-14 3:34am    
Nice ......
NorouziFar 7-Oct-14 22:13pm    
nono
no

change "ConnectionStrings" no "AppSettings" ??????????
ConnectionStrings
ConnectionStrings
ConnectionStrings

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900