好的,这就是答案,我有完全相同的情况。我想写一个 winforms 应用程序来允许普通用户更新 web.config。您必须以一种愚蠢的方式获取配置...
// the key of the setting
string key = "MyKey";
// the new value you want to change the setting to
string value = "This is my New Value!";
// the path to the web.config
string path = @"C:\web.config";
// open your web.config, so far this is the ONLY way i've found to do this without it wanting a virtual directory or some nonsense
// even "OpenExeConfiguration" will not work
var config = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap() { ExeConfigFilename = path }, ConfigurationUserLevel.None);
// now that we have our config, grab the element out of the settings
var element = config.AppSettings.Settings[key];
// it may be null if its not there already
if (element == null)
{
// we'll handle it not being there by adding it with the new value
config.AppSettings.Settings.Add(key, value);
}
else
{
// note: if you wanted to you could inspect the current value via element.Value
// in this case, its already present, just update the value
element.Value = value;
}
// save the config, minimal is key here if you dont want huge web.config bloat
config.Save(ConfigurationSaveMode.Minimal, true);
这是一个例子
之前:
<?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>
之后:
<?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>
请注意,如果您给它一个无效的路径,它只会在该路径/文件名处创建一个配置文件。基本上,首先对其进行 File.Exists 检查
顺便说一句,当您使用它时,您可以在 web.config 中编写一个代表您的设置的类。完成此操作后,编写您的 getter/setter 以读取/写入 web.config 中的设置。完成此操作后,您可以将此类添加为数据源并将数据绑定控件拖到您的 winform 上。这将为您提供一个完全数据绑定的 winform web.config 编辑器,您可以在几分钟内轻松完成。 我有一个工作中的例子,我明天会发布。
功能齐全的winforms解决方案
所以这是编写 GUI 来编辑 web.config 的一个相对简单的解决方案,有些人可能会说它过于复杂,而记事本也可以,但它适用于我和我的观众。
它基本上如上所述工作,我编写了一个具有我想要作为属性的配置点的类。 ctor 从路径打开文件,getter / setter 将数据从返回的配置对象中拉出,最后它有一个保存方法将其写出。有了这个类,我可以将类添加为数据源并将绑定控件拖放到 winforms 上。从那里你所要做的就是连接一个按钮来调用你的类的保存方法。
配置类
using System.Configuration;
// This is a representation of our web.config, we can change the properties and call save to save them
public class WebConfigSettings
{
// This holds our configuration element so we dont have to reopen the file constantly
private Configuration config;
// given a path to a web.config, this ctor will init the class and open the config file so it can map the getters / setters to the values in the config
public WebConfigSettings(string path)
{
// open the config via a method that we wrote, since we'll be opening it in more than 1 location
this.config = this.OpenConfig(path);
}
// Read/Write property that maps to a web.config setting
public string MySetting
{
get { return this.Get("MySetting"); }
set { this.Set("MySetting", value); }
}
// Read/Write property that maps to a web.config setting
public string MySetting2
{
get { return this.Get("MySetting2"); }
set { this.Set("MySetting2", value); }
}
// helper method to get the value of a given key
private string Get(string key)
{
var element = config.AppSettings.Settings[key];
// it may be null if its not there already
if (element == null)
{
// we'll handle it not being there by adding it with the new value
config.AppSettings.Settings.Add(key, "");
// pull the element again so we can set it below
element = config.AppSettings.Settings[key];
}
return element.Value;
}
// helper method to set the value of a given key
private void Set(string key, string value)
{
// now that we have our config, grab the element out of the settings
var element = this.config.AppSettings.Settings[key];
// it may be null if its not there already
if (element == null)
{
// we'll handle it not being there by adding it with the new value
config.AppSettings.Settings.Add(key, value);
}
else
{
// in this case, its already present, just update the value
element.Value = value;
}
}
// Writes all the values to the config file
public void Save()
{
// save the config, minimal is key here if you dont want huge web.config bloat
this.config.Save(ConfigurationSaveMode.Minimal, true);
}
public void SaveAs(string newPath)
{
this.config.SaveAs(path, ConfigurationSaveMode.Minimal, true);
// due to some weird .net issue, you have to null the config out after you SaveAs it because next time you try to save, it will error
this.config = null;
this.config = this.OpenConfig(newPath);
}
// where the magic happens, we'll open the config here
protected Configuration OpenConfig(string path)
{
return ConfigurationManager.OpenMappedExeConfiguration(
new ExeConfigurationFileMap() { ExeConfigFilename = path },
ConfigurationUserLevel.None);
}
}
构建,然后从那里您可以转到您的 winform 设计器,转到数据 > 显示数据源 (Shift+Alt+D)。右击>添加新数据源,如图所示添加为对象
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
将它(WebConfigSettings,最顶部)拖到 winform 上。就我而言,我将删除导航器,因为它是一个列表,而我只有一个。
Freshly added databound controls http://img96.imageshack.us/img96/8268/29648681.png
你应该在设计器的底部有类似 webConfigSettingsBindingSource 的东西(如下图所示)。转到代码视图并将ctor 更改为此
public Form1()
{
InitializeComponent();
// wire up the actual source of data
this.webConfigSettingsBindingSource.DataSource = new WebConfigSettings(@"c:\web.config");
}
在您的 winform 中添加保存按钮
Save button added http://img402.imageshack.us/img402/8634/73975062.png
添加以下事件处理程序
private void saveButton_Click(object sender, EventArgs e)
{
// get our WebConfigSettings object out of the datasource to do some save'n
var settings = (WebConfigSettings)this.webConfigSettingsBindingSource.DataSource;
// call save, this will write the changes to the file via the ConfigurationManager
settings.Save();
}
现在,您有了一个不错的简单数据绑定 web.config 编辑器。要添加/删除字段,您只需修改 WebConfigSettings 类,刷新数据源窗口中的数据源(构建后),然后将新字段拖放到 UI 上。
您仍然需要连接一些代码来指定要打开的 web.config,对于这个示例,我只是硬编码了路径。
这里最酷的地方在于 GUI 添加的所有价值。您可以轻松添加目录或文件浏览器对话框,您可以拥有连接字符串测试器等。所有这些都非常容易添加并且对最终用户非常强大。