Read and Edit web.config file in C#

Read and Update web.config file is one of the important and regular task from server side code or from different application. We can Read and Write web.config’s AppSetting or SQL ConnectionString using two classes WebConfigurationManager and ConfigurationManager in C#. In this article, I am going to write C# code examples to Read and Edit Web config settings at runtime and from different application(i.e -Setup).
 

Read web.config file in C#

Consider the sample web.config file structure:

<configuration>
  <appSettings>
    <add key="appPath" value="C:\Prgram FilesMyApplication" />
  </appSettings>
  <connectionStrings>
    <add name="sqlInfo" connectionString="Data Source=.SQLExpress; 
Initial Catalog=MorganDB2; Integrated Security=SSPI;" />
  </connectionStrings>
  </configuration>

Read web.config’s AppSetting and ConnectionString using WebConfigurationManager:

private void ReadWebConfigSetting()
{
    string appPath = WebConfigurationManager.AppSettings["appPath"];
    string connString = WebConfigurationManager.ConnectionStrings["sqlInfo"].ToString();
}

Read web.config’s AppSetting and ConnectionString using ConfigurationManager:

private void ReadWebConfigSettingbyCM()
{
    string appPath = ConfigurationManager.AppSettings["appPath"];
    string connString = ConfigurationManager.ConnectionStrings["sqlInfo"].ToString();
}

Write web.config setting using C#

Edit web.config’s AppSetting and ConnectionString from Different Application:

Use the below sample code to write or edit a web.config’s AppSetting and ConnectionString from different/other location.

private static void EditWebConfigFile()
{
    string webDirPath = @"C:\Prgram FilesMyAppWebFiles";
    var vdm = new VirtualDirectoryMapping(webDirPath, true, "web.config");
    var wcfm = new WebConfigurationFileMap();
    wcfm.VirtualDirectories.Add("/", vdm);
    var configuration = WebConfigurationManager.OpenMappedWebConfiguration(wcfm, "/");

    var appSettingsSection = (AppSettingsSection)configuration.GetSection("appSettings");
    appSettingsSection.Settings["appPath"].Value = @"C:\Prgram FilesMyApp";

    var connStrSection = (ConnectionStringsSection)configuration.GetSection("connectionStrings");
    connStrSection.ConnectionStrings["sqlInfo"].ConnectionString = @"Data Source=.SQLExpress;
 Initial Catalog=MorganDB; Integrated Security=SSPI;";

    configuration.Save(ConfigurationSaveMode.Modified);
}

Edit web.config’s AppSetting and ConnectionString at Run Time:

Use the below sample code to edit and update a web.config’s AppSetting and ConnectionString at run time.

private void WriteWebConfigSettings()
{
    var configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");

    var appSettingsSection = (AppSettingsSection)configuration.GetSection("appSettings");
    appSettingsSection.Settings["appPath"].Value = @"C:\Prgram FilesMyApplication";

    var connStrSection = (ConnectionStringsSection)configuration.GetSection("connectionStrings");
    connStrSection.ConnectionStrings["sqlInfo"].ConnectionString = @"Data Source=.SQLExpress; 
Initial Catalog=MorganDB2; Integrated Security=SSPI;"; ;

    configuration.Save(ConfigurationSaveMode.Modified);
}

 



Advertisement

Leave a Comment