.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> Windows Application >> Post New QuestionBookmark and Share Subscribe to Forum

How to modify App.config file dynamically?

Posted By: Gopal     Posted Date: April 06, 2010    Points:2   Category :Windows Application
Hi,

How to modify App.config file dynamically? For a windows application I need to modify SName dynamically at run time depending upon some condition.

I have the App.config file like this

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="SName" value="Gopal"/>
</appSettings>
</configuration>

Just i want to change the "SName" value to "Rama" from "Gopal".



I am using C# as code behind.

Thanks in advance
Gopal

Responses
Author: Dilip             
Posted Date: April 06, 2010     Points: 5   

Check this out

http://www.codeproject.com/KB/cs/SystemConfiguration.aspx

Regards
Dilip
Author: Venkat          Accepted Answer   
Posted Date: April 06, 2010     Points: 10   

hi,

To change the configuration value at runtime you can try this code. This will modify the config file generated by Visual Studio while compiling the code in Dedug or release folder.

Configuration configFile = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
configFile.AppSettings.Settings["MyString"].Value = "Hello World";
configFile.Save();


Don't forget to add the System.Configuration Namespace and add the reference assembly.

Mark As Answer if this solve your problem
Warm Regards,
Venkat
Author: Gopal             
Posted Date: April 06, 2010     Points: 5   

Hi Dilip


Thanks for your response. As you mentioned the url i checked that codeproject.com,but i didn't get any thing in my App.config file. It was as is it is as just like before. There is no change in my App.config.
If there is any chance plz let me know...
Thank u...

Author: Gopal             
Posted Date: April 06, 2010     Points: 5   

Hi Venkat



Thanks for your code and your response. I tried that code in before but is not showing any error or anything. But the App.config file is as it is......... There is no change in my App.onfig file.
How to modify that and save the content which i was changed.....

I have the App.config file like this

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="SName" value="Gopal"/>
</appSettings>
</configuration>

Just i want to change the "SName" value to "Rama" from "Gopal".


Please post the code if u have any......

Thank you...

Author: Venkat          Accepted Answer   
Posted Date: April 06, 2010     Points: 10   

Hi Gopal,

I have checked the code and its working. When we compile the project your app.config file is compiled into yourProjectName.Exe.Config file into debug or release folder and application exe read from this file only at runtime for configuration values.

Apply the above code and Take a look at the Debug or Release folder of your application and see the yourProjectName.Exe.Config file.

Mark As Answer if this solve your problem
Warm Regards,
Venkat
Author: Gopal             
Posted Date: April 07, 2010     Points: 5   

Hi Venkat............


I am really thankful to you. I got the code which i want....... Thanks a lot...


Regards,
Gopal.

Author: Venkat          Accepted Answer   
Posted Date: April 07, 2010     Points: 10   

Hi Gopal.

You are most welcome..

Mark As Answer if this solve your problem
Warm Regards,
Venkat
Author: Gopal             
Posted Date: April 07, 2010     Points: 5   

Hi Venkat.....

How to open that recompiled config file?? How can i get the values from that??

Thnaks

Author: Sagar Pardeshi             
Posted Date: April 07, 2010     Points: 5   

 public void SetConfig(String path, int configType)
{
if (configType == 0)
{
WebConfigurationFileMap fileMap = new WebConfigurationFileMap();
fileMap.VirtualDirectories.Add("/", new VirtualDirectoryMapping(path, true));
config = WebConfigurationManager.OpenMappedWebConfiguration(fileMap, "/");
}
else
{
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = path + "app.config";
config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

}
HttpContext.Current.Session["config"] = config;
}


public void SaveValue(String sectionName, String Key, String Value)
{
config = (Configuration)HttpContext.Current.Session["config"];
if (sectionName == "appSettings")
{
AppSettingsSection sec = config.AppSettings;
if (sec.Settings[Key] != null)
{
sec.Settings[Key].Value = Value;
}
else
{
sec.Settings.Add(Key, Value);

}
}
else
{
ConnectionStringsSection sec = config.ConnectionStrings;
if (sec.ConnectionStrings[Key] != null)
{
sec.ConnectionStrings[Key].ConnectionString = Value;
}
else
{
ConnectionStringSettings set = new ConnectionStringSettings();
set.Name = Key;
set.ConnectionString = Value;
sec.ConnectionStrings.Add(set);
}
}
config.Save();
HttpContext.Current.Session["config"] = config;
}


Author: Venkat             
Posted Date: April 07, 2010     Points: 5   

Hi,

You can open in Notepad, or Visual Studio itself.

Mark As Answer if this solve your problem
Warm Regards,
Venkat


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend