Software Programs and Projects

This Software Programs and Projects include programing tips and technology using different languages like VC++,ASP,JSP C#.Net,PHP,VB.Net,JavaScript,ASP.NET .Software Programs and Projects blog mainly support to software programmers and provide help through the mail.

We can edit,delete,add keys and values into Ap.config using C#.NET ?

First off all using system.configuration.dll (if not exist) then using namespace System.Configuration

Then add app.config file

that file contain only appSettings tag


Call this Function "SaveConfigValue"


SaveConfigValue(strkeyDatabaseType, strType);
SaveConfigValue(strkeyServer, strServer);
SaveConfigValue(strkeyUsername, strUserName);
SaveConfigValue(strkeyPassword, strPass);
SaveConfigValue(strkeyDatabase, strDatabase);


#region Save Database Settings into Config File
///


/// Save Database Settings into Config File
///

///
///
///
private bool SaveConfigValue(string strKey, string strValue)
{
bool bRet = true;
try
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Remove(strKey);
config.AppSettings.Settings.Add(strKey, strValue);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
bRet = false;
}
return bRet;
}
#endregion

#region Load DataBaseSettings
///
/// Load Database settings from Config File
///

///
private bool LoadConfigValue()
{
bool bRet = true;
try
{
ConfigurationManager.RefreshSection("appSettings");
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
foreach (string strKey in config.AppSettings.Settings.AllKeys)
{
string strVal = ConfigurationManager.AppSettings[strKey].ToString();
switch(strKey)
{
case strkeyDatabaseType:
{
if (strVal == strDatabaseTypeAccess)
{
radioButtonAccess.Checked = true;
}
else
{
radioButtonSqlServer.Checked = true;
}
break;
}
case strkeyServer:
txtServer.Text = strVal;
break;
case strkeyUsername:
txtUserName.Text = strVal;
break;
case strkeyPassword:
txtPassword.Text = strVal;
break;
case strkeyDatabase:
txtConnectionString.Text = strVal;
break;
}
}
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
bRet = false;
}
return bRet;
}
#endregion


#tag:-Add,Delete,Edit App.config File Using C#.Net