Background:
I have some data thats stored in the web.config files of about 100 web applications. This data is getting moved to a database gradually. The webpages will show the web.config data until somebody clicks on an "edit" link in which case they'll be redirected to a webpage which will allow them to update this data where it will be saved in a database instead.
Problem:
Not all of the data will be changed on this page that will save it to the database. When somebody clicks the "edit" link I want the form to populate with the data from the web.config file and when they click "save" have it save to the database. However, using the configurationmanager I can only get it to pull data from the web.config file on current application.
Questions:
Is there a way to use configurationmanager to select the web.config file from lets say ../{dynamic_app_id}/web.config ?
is reading them as plain xml files my only option?
Are there any pitfalls to this approach?
Is there another solution that would work better perhaps?
You can read any config file with ease. Please see my sample code where I read application settings from external app.config file:
System.Configuration.KeyValueConfigurationCollection settings;
System.Configuration.Configuration config;
System.Configuration.ExeConfigurationFileMap configFile = new System.Configuration.ExeConfigurationFileMap();
configFile.ExeConfigFilename = "my_file.config";
config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(configFile, System.Configuration.ConfigurationUserLevel.None);
settings = config.AppSettings.Settings;
Happy coding and best regards!
You can add below section in your web.config
then, add "env" folder in your project and add your environmental settings into EnvironmentalSettings.config. And you can still use ConfigurationManager to get settings from EnvironmentalSettings file.
Does that answer your question?
Related
I am new to .net and C# and facing difficulty in modifying app.config.
In my app.config file, I have:
<UserInfo Domain = "domain" UserName = "Name" Password = "password">
Normally this information is given manually, when asked for, in the application. Is there anyway to modify the C# code such that this information is directly taken from an excel file with the required details. The application right now does not use or work with MS-excel.
If you wish to use excel file, then you have to parse the excel file using third party libraries. If that seems complex to you, use CSV file instead.
or Create a separate page for administering this username and password in your app. In that page save new username/password in web.config programmatically.
To know more see this question
Change a web.config programmatically with C# (.NET)
I want to read and write the users settings in the app.config file of my project.
I want to add the user name as a section and adding its setting, for each user if he is not exist in the app.config.
And I want to read it at the beginning of the application.
Can you help me please?
.Net already has this class ConfigurationManager you can use to read app.config and web.config for web scenarios. You could use something like this
var temp = ConfigurationManager.AppSettings["data"];
for more info please check the MSDN ConfigurationManager
I have a project that is NOT a website project, so there is no web.config file.
I want to connect to my database. How can I do this when I don't have the web.config file to tell the database name, url, password etc?
By searching I found this: ASP.NET user database without web.config connection strings
But I don't know if this is what I need. I didn't experience from it anyway.
Thanks
Ps. It is a wpf project.
By the following way, you can define connection strings to connect with database without using web.config file.
SqlConnection con = new SqlConnection("Server=localhost\\SQLEXPRESS;Integrated security=SSPI;database=master;");
If it's not a Web application, you should have an app.config that will work the same way.
I'm developing a software in .NET 4.0, which reads and writes application settings. On finish the software will be stored on a file server. No local instances will be executed.
By default the user settings XML file is stored in every users AppData\... directory, but I want to change the file location to the same directory the executable is stored.
This means, that all users should use the same XML user-settings file with modified contents.
I've read this question and answeres where a user describes how to realize that in JSON format.
Question:
Isn't there any other (simple) way to tell the Settings class, where to read from and write to user settings?
The following has been discussed:
Users will always have enough access rights to modify the settings file.
Modifications on settings should be picked up by other users.
Users will start the application from different network computers.
I could implement my own XML file handled by the application (I'll keep this in mind).
I'm not sure if you can get the functionality that you want with the standard Settings class. But I do think that the end result your searching for can be accomplished.
If you want changes in settings user 1 makes to be instantly enforced for user 2, you should look at storing the settings in a database. Your application can then be periodically check this table for changes. For instance if user 1 changes the color of a control, then every time user 2 loads a screen with that control you check the database for the color.
Or, if you want the settings to be applied on start-up of you application. Use a datacontract + xml serializer to write settings to a file of your choosing on a network accessible path/folder. Then make sure you can handle read/write locking of this file.
These are just general ideas that I think you should consider. I dont claim these are your only options though. If you whish to pursue any of these things there are a bunch of blogs and stackoverflow pages with all the information you need.
good luck!
I added a custom Application Configuration File called AppSettings.config to my project and set its Copy to output property to Copy if newer (this copies the config file into the executables folder on building the project.
My settings must be classified as appSettings, not applicationSettings (non-editable) or userSettings (stored in the users AppData folder). Here's an example:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<!-- single values -->
<add key="Name" value="Chris" />
<add key="Age" value="25" />
<!-- lists, semi-colon separated-->
<add key="Hobbies" value="Football;Volleyball;Wakeboarding" />
</appSettings>
</configuration>
Look at the code below, how to access and modify them:
Configuration AppConfiguration =
ConfigurationManager.OpenMappedExeConfiguration(
new ExeConfigurationFileMap {ExeConfigFilename = "AppSettings.config"}, ConfigurationUserLevel.None);
var settings = AppConfiguration.AppSettings.Settings;
// access
var name = settings["Name"].Value;
var age = settings["Age"].Value;
var hobbies = settings["Hobbies"].Value.Split(new[]{';'});
// modify
settings["Age"].Value = "50";
// store (writes the physical file)
AppConfiguration.Save(ConfigurationSaveMode.Modified);
I used the built-in app.config for default application settings and wanted to separate them from the editable global settings in AppSettings.config. That's why I use a custom config file.
That's not directly an answer to the question, but it solves the problem on how to store and share editable application settings in custom configuration files.
Hope it helps any other users! :-)
I want to create setup of my web application with editable web.config file content.
Any help?
If you are trying to change the config with your code, the following should help. Any time you execute the following code, your site will be restarted. So keep that in mind.
var config = (Configuration)WebConfigurationManager.OpenWebConfiguration("~");
var configSection = (MySectionTypeHere)myConfiguration.GetSection("system.web/section");
//make your edits here
myConfiguration.Save();
If this is not what you're looking for, perhaps you could provide some more information.
You could use a tool like MS Web deploy. Read more about it here. http://learn.iis.net/page.aspx/578/package-an-application-for-the-windows-web-application-gallery/