So, here is the answer, I have EXACTLY the same scenario. I wanted to write a winforms application so that ordinary users can update web.config. You have to go about configuring goofy ...
Here is an example of what he is doing
Before:
<?xml version="1.0"?> <configuration> <appSettings> <add key="MyKey" value="OldValue" /> </appSettings> <connectionStrings> <add name="myConnString" connectionString="blah blah blah" providerName="System.Data.SqlClient" /> </connectionStrings> </configuration>
After:
<?xml version="1.0"?> <configuration> <appSettings> <add key="MyKey" value="This is my New Value!" /> </appSettings> <connectionStrings> <add name="myConnString" connectionString="blah blah blah" providerName="System.Data.SqlClient" /> </connectionStrings> <system.web> <trust level="Full" /> <webControls clientScriptsLocation="/aspnet_client/{0}/{1}/" /> </system.web> </configuration>
Just be careful if you give it the wrong path, it will simply create a configuration file for that path / file name. Basically, check the .Exists file first.
By the way, while you are on it, you can write a class that represents your settings in your web.config. When you do this, write your getters / setters to read / write settings in the web.config file. When this is done, you can add this class as a data source and drag the data controls onto your winform. This will give you a fully edited winform web.config editor that you can easily pull out in minutes. I have an example at work that I will post tomorrow.
Full-featured winforms solution
So, this is a relatively simple solution to write Gui for editing web.config, some may say that it is too complicated when the notebook works fine, but it works for me and my audience.
It basically works, as described above, I wrote a class in which there were configuration parameters that I wanted to use as properties. ctor opens the file from the path, and getters / setters pull data from the returned configuration object, and in the end it has a save method that writes it. With this class, I can add the class as a data source and drag and drop related controls onto winforms. From there, all you have to do is plug in a button that calls the save method in your class.
Configuration class
using System.Configuration;
Build, and then from there you can simply go to your winform design, go to Data> Show Data Sources (Shift + Alt + D). Right click> Add New Data Source and add it as an object as shown
Data Source Configuration Wizard 1 of 2 http://img109.imageshack.us/img109/8268/98868932.png
Data Source Configuration Wizard 2 of 2 http://img714.imageshack.us/img714/7287/91962513.png
Drag it (WebConfigSettings, topmost) onto winform. In my case, I will delete the navigator, as it is for the list, and I have it.
Recently Added Data Controls http://img96.imageshack.us/img96/8268/29648681.png
You should have something like webConfigSettingsBindingSource at the bottom of the constructor (shown in the following figure). Go to the code view and change ctor to
public Form1() { InitializeComponent(); // wire up the actual source of data this.webConfigSettingsBindingSource.DataSource = new WebConfigSettings(@"c:\web.config"); }
Add save button to your winform
Added save button http://img402.imageshack.us/img402/8634/73975062.png
Add the following event handler
private void saveButton_Click(object sender, EventArgs e) {
There, you now have a simple, simple web.config data editor. To add / remove fields, you simply change your WebConfigSettings class, update the data source in the "Data Sources" window (after assembly), and then drag n to remove new fields in the user interface.
You still have to include some code that indicates the opening of web.config, for this example I just encoded the path.
The nice thing is all that adds a graphical interface. You can easily add directory or file dialogs, you can have connection string testers, etc. Everything is very easy to add and very powerful for the end user.