Friday, 24 May 2013

Edit Web.config File Using C# .NET

we can Programatically Change Web.config File Using C#  .NET.

ASP.NET Provides Two namespaces for Configuration Settings
System.Configuration, System.Web.Configuration.

So If we want to Change the Any Mail Server Settings and ConnectionString Settings, We can do all these can Modify easily instead opening the File and Changes and Saving Back. Instead of Doing all These Things, Create an UI for Changing All these Things for Admin so that web.config file can change from that Place Only.

Here is the sample Example for Changing the SMTP Server  IP Address.

Using the namespaces of configuration:




using System.Configuration;

using System.Web.Configuration;

 

Config File:

 

<appSettings>

<addkey="SMTPServer" value="64.59.71.11" />

</appSettings>

C#:

 

Configuration ConfigObj = WebConfigurationManager.OpenWebConfiguration("~");

AppSettingsSection AppObj = (AppSettingsSection)ConfigObj.GetSection("appSettings");

if (AppObj != null)

{

AppObj.Settings["SMTPServer"].Value = txtSmtpIP.Text;

ConfigObj.Save();

}




 

 


 

 
 

No comments:

Post a Comment