Winforms 如何编辑外部web.config文件?

Winforms 如何编辑外部web.config文件?,winforms,configuration,web-config,Winforms,Configuration,Web Config,我正在尝试编写一个winform应用程序,该应用程序能够编辑已安装web应用程序的web.config文件。 我已经阅读了ConfigurationManager和WebConfigurationManager类方法,但我不确定如何打开web应用程序的配置文件并对其进行编辑 我正在寻找一种不需要将配置文件作为常规XmlDocument加载的方法,尽管如果这是唯一可用的选项,我愿意这样做 任何建议都将不胜感激。所有app.config和web.config都只是XML文件。您可以使用XMLDocu

我正在尝试编写一个winform应用程序,该应用程序能够编辑已安装web应用程序的web.config文件。 我已经阅读了ConfigurationManager和WebConfigurationManager类方法,但我不确定如何打开web应用程序的配置文件并对其进行编辑

我正在寻找一种不需要将配置文件作为常规XmlDocument加载的方法,尽管如果这是唯一可用的选项,我愿意这样做


任何建议都将不胜感激。

所有
app.config
web.config
都只是XML文件。您可以使用XMLDocument、XMLWriter等打开和编辑它们。

好的,下面是答案,我有完全相同的场景。我想编写一个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>
构建,然后从那里您可以转到winform设计器,转到数据>显示数据源(Shift+Alt+D)。右键单击>添加新数据源并将其作为对象添加,如图所示

将它(最上面的WebConfigSettings)拖到winform上。在我的例子中,我将删除导航器,因为这是一个列表,我只有一个

在设计器的底部应该有类似webConfigSettingsBindingSource的内容(如下图所示)。转到代码视图,并将
ctor
更改为此

public Form1()
{
    InitializeComponent();

    // wire up the actual source of data
    this.webConfigSettingsBindingSource.DataSource = new WebConfigSettings(@"c:\web.config");
}
将保存按钮添加到winform

添加以下事件处理程序

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增加的所有价值。您可以轻松添加目录或文件浏览器对话框,您可以使用连接字符串测试器等。所有这些都非常容易添加,对最终用户来说非常强大。

我强烈建议您使用
XElement
以及启用LINQ(LINQ到XML)的工具

例如,您想要更改
连接字符串
。这种代码已经足够好了

var connString = from c in webConfigXElement.appSettings.connectionString
                        where c.name == "myConnection"
                        select c;
现在您可以完全控制
元素,并可以对其执行任何操作

我指的是你的学习和即时工作


希望这能帮助您轻松地完全控制
.xml

在这里,我编写了一个玩具应用程序(VB.NET Windows客户端),它使用树/网格来导航和编辑xml文件

你可能会从中得到一些想法。如果您想在web.config上试用,它的VS project文件是或只是一个MSI来安装它


它将文件加载到一个数据集(DataSet.ReadXML())中,该数据集将文件解析为数据表,然后在标准DataGrid中显示并允许编辑内容。然后,它会将编辑的内容保存回XML文件(DataSet.WriteXML())。

他明确表示不想这样做。这里的每个人都知道web.config是xml,您可以以任何合适的方式打开和编辑它。@Allen
ConfigurationManager
class确实用于修改/添加。您还应该记住,
ConfigurationManager
类可以控制
AppSettings
ConnectionStrings
XmlDocument
是一种痛苦,而
ConfigurationManager
.config
文件的解决方案之一。相信我,
linqtoxml
在您需要电源时更方便。顺便说一下,艾伦,你应该知道你的举止。你应该知道什么时候该投赞成票,什么时候该投反对票。虽然你的帖子回答了这个问题,但问题明确地说他们不想使用通用的XMLDocument对象(这是个好主意,因为你不需要重新发明轮子)。
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();
}
var connString = from c in webConfigXElement.appSettings.connectionString
                        where c.name == "myConnection"
                        select c;