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
Articles
- Articles(C#.NET) (3)
- ASP.NET (6)
- Asp.Net(Email) (1)
- Asp.net(Image) (2)
- Asp.Net(Web.Config) (2)
- C#.NET (5)
- C#.NET Threading (3)
- C#.NET(ASP.NET) (4)
- Comments in PHP (1)
- Encryption In PHP (1)
- iPhone (Articles) (1)
- JavaScript (1)
- Json with PHP (1)
- LINQ (1)
- PHP (2)
- PHP Constant (1)
- PHP Operators (1)
- PHP Print Command (1)
- PHP Tutorial (2)
- Strings In PHP (1)
- Variable Declaration In PHP (1)
- WPF (1)
- XAML (2)
About Me
Help Link
Followers
Posted by
Sreejith A.K
Saturday, September 19, 2009
comments (0)
Subscribe to:
Posts (Atom)