Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How we can add data or edit web.config file in asp dot net.
Posted
Comments
StianSandberg 23-Apr-13 4:54am    
As the filename says, it's a config file. You really should not add data to that file at runtime. What kind of data are you trying to add?
RoelofJanster 23-Apr-13 4:57am    
I am doing an SDK integration and there is already a config file but it doesn't contain the connectionstrings section. I need to add that at run time if it doesn't exist(to avoid user interference with the config file). It is an Windows Application.

No idea why you want this. Because editing the web.config will recycle the AppPool and application will be restarted.

Still , use Edit and Encrypt Web.Config Sections Using C# 2.0[^]
 
Share this answer
 
Comments
_Amy 23-Apr-13 5:26am    
5'd
bbirajdar 23-Apr-13 5:32am    
Thank you _Amy
C#
// You will have to load the config somehow. Either by the exe filename or the config filepath directly 
var cfg = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
var setting = new ConnectionStringSettings
                    {
                        ConnectionString = "hello=123",
                        Name = "123test"
                    }
cfg.ConnectionStrings.ConnectionStrings.Add(setting);
cfg.Save();


you can load a web.config file by using WebConfigurationManager.OpenWebConfiguration Method[^]
 
Share this answer
 
v2
Comments
_Amy 23-Apr-13 5:26am    
5'd.
StianSandberg 23-Apr-13 5:56am    
thank you :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900