当前位置:  开发笔记 > 后端 > 正文

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

如何解决《如何编辑外部web.config文件?》经验,为你挑选了1个好方法。

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

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

任何意见,将不胜感激.



1> Allen Rice..:

好的,这就是答案,我有完全相同的情况.我想编写一个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);

这是它的作用的一个例子

之前:



  
    
  
  
    
  

后:



  
    
  
  
    
  
  
    
    
  

要小心,如果你给它一个无效的路径,它只会在该路径/文件名创建一个配置文件.基本上,首先要对File.Exists进行检查

顺便说一句,当你在它的时候,你可以在web.config中编写一个代表你的设置的类.执行此操作后,编写getter/setter以读取/写入web.config中的设置.完成此操作后,您可以将此类添加为数据源,并将数据绑定控件拖到您的winform上.这将为您提供一个完全数据绑定的winform web.config编辑器,您可以在几分钟内轻松完成. 我有一个工作的例子,明天我会发布.

功能齐全的winforms解决方案

所以这是编写一个Gui来编辑web.config的一个相对简单的解决方案,有些人可能会说当记事本做得很好时它过于复杂但它适用于我和我的观众.

它基本上如上所述工作,我写了一个具有我想要的配置点作为属性的类.在ctor打开从路径和干将文件/ setter方法拉出来的数据返回的配置对象,最后它有把它写到一个保存方法.通过这个类,我可以将类添加为数据源,并将绑定控件拖放到winforms上.从那里你要做的就是连接一个在你的班级上调用save方法的按钮.

配置类

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).右键单击>添加新数据源并将其添加为对象,如图所示

数据源配置向导1 of 2 http://img109.imageshack.us/img109/8268/98868932.png

数据源配置向导2 of 2 http://img714.imageshack.us/img714/7287/91962513.png

将它(WebConfigSettings,最顶层)拖到winform上.在我的情况下,我将删除导航器,因为它是一个List,我只有一个.

新添加的数据绑定控件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中添加一个保存按钮

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

推荐阅读
coco2冰冰
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有