【问题标题】:Changing values in Web.config with a Batch file or in .NET code使用批处理文件或 .NET 代码更改 Web.config 中的值
【发布时间】:2009-03-19 12:20:36
【问题描述】:

我的电脑上有一个 web.config 文件。

我需要在文件中更改和添加很多东西。 (我实际上正在使用我的 SharePoint web.config 文件)

我可以用批处理文件来做这件事吗,如果可以,我该怎么做。 或者我将如何使用 VB.NET 或 C# 代码来做到这一点?

各位有什么想法吗?

编辑:我需要创建一个程序来改变一个 web.config,比如说我的 web.config 放在我的桌面上,而不是我项目的实际 web.config

问候 艾蒂安

【问题讨论】:

    标签: c# .net asp.net vb.net sharepoint


    【解决方案1】:

    您可以从 C# 代码中修改它,例如:

    Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");     
    AppSettingsSection appSettingsSection = (AppSettingsSection)configuration.GetSection("appSettings"); 
    
    if (appSettingsSection != null) 
    {
      appSettingsSection.Settings["foo"].Value = "bar"; 
      config.Save();
    }
    

    显然,其中 foo 是键,而 bar 是要设置的键的值。要删除一个值,请使用 Settings.Remove(key);

    请参阅msdn documentation 了解有关 OpenWebConfiguration 方法的更多信息等。

    【讨论】:

    • 谢谢,但我需要创建一个程序来更改 web.config,比如说我的 web.config 放在我的桌面上,而不是我项目的实际 web.config....我这样做?
    【解决方案2】:

    您要更改文件的上下文确实会影响您应该如何做。如果您正在考虑相对频繁地执行更改,但在管理域中,那么某种命令行工具是有意义的,在这种情况下,我同意 JaredPar 的观点,即PowerShell 将是一个有价值的工具。

    另一方面,如果您发现自己需要在更加程序化的环境中修改 web.config(例如,作为安装程序的一部分),那么使用程序化技术可能更有意义。我最近不得不做这样的事情,Linq to Xml 证明非常方便。

    例如,要打开文档“C:\foo\bar.xml”,您可以执行以下操作(未经测试,目前没有方便的构建环境):

    XDocument config = XDocument.Load(@"C:\foo\bar.xml");
    

    然后您可以使用API 以通常的方式继续操作。请注意,如果您正在执行管理任务而不是编程任务,这可能会有点矫枉过正——学习 PowerShell 之类的工具有很大的长期优势。

    最后,如果您正在使用 web.config 的程序中修改 web.config,并且您没有做任何太花哨或动态的事情,那么使用内置的 SettingsConfigurationManager 可能是要走的路。

    【讨论】:

    • 感谢您的回复....我需要创建一个程序来更改 web.config,比如说我桌面上的 web.config,而不是我项目的实际 web.config
    • Powershell 和 linq 仍然可以完全胜任。
    • 嗨,我现在看了一下。我必须用 LINQ 打开我的 web.config 吗?如果是这样,你能给我一个例子,说明我的代码必须是什么样子才能打开位于 C:\MyFile\web.config 上的 web.config ......
    【解决方案3】:

    最好的办法是使用 MSBuild 脚本和 MsBuild Community Tasks XML 批量更新任务来更改它

    【讨论】:

      【解决方案4】:

      我个人建议使用 PowerShell。这是 Microsoft 的下一代命令行,它位于 .Net 之上。它被构建用于跨大量文件进行批量编辑等项目。

      【讨论】:

        【解决方案5】:

        对于 SharePoint 环境中的 web.config 更改,您有专门为此任务开发的类。您只需搜索SPWebConfigModification class

        【讨论】:

          【解决方案6】:

          加载任意 .NET 配置文件

          string configLocation = @"C:\myconfigFile.Config";
          ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
          
          
          configFileName = configLocation;
          configFileMap.ExeConfigFilename = configFileName;
          
          Configuration configuration= ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
          

          然后使用Razzie's code 更改实际的配置设置

          AppSettingsSection appSettingsSection = (AppSettingsSection)configuration.GetSection("appSettings"); 
          if (appSettingsSection != null) 
          {  
              appSettingsSection.Settings["foo"].Value = "bar";   
              configuration.Save();
          }
          

          【讨论】:

          • 您好,感谢您的回复。 configFileName 是我的配置文件的名称 ----- 意思是“Web.Config”吗?
          【解决方案7】:

          这是我需要做的......感谢所有的帮助!!!

          // Read in Xml-file 
                  XmlDocument doc = new XmlDocument();
                  doc.Load("C:/Web.config");
          
                  //SaveControl tag..........................................................
                  XmlNode n = doc.SelectSingleNode("/configuration/SharePoint/SafeControls");
          
                  XmlElement elemWeb = doc.CreateElement("SafeControl");
                  elemWeb.SetAttribute("Assembly", "SamrasWebOption4");
                  elemWeb.SetAttribute("Namespace", "SamrasWebOption4");
                  elemWeb.SetAttribute("TypeName", "*");
                  elemWeb.SetAttribute("Safe", "True");
          
                  XmlElement elemSmartPart = doc.CreateElement("SafeControl");
                  elemSmartPart.SetAttribute("Assembly", "Machine_Totals");
                  elemSmartPart.SetAttribute("Namespace", "Machine_Totals");
                  elemSmartPart.SetAttribute("TypeName", "*");
                  elemSmartPart.SetAttribute("Safe", "True");
          
                  //Appending the Nodes......................................................
                  n.AppendChild(elemWeb);
                  n.AppendChild(elemSmartPart);
          
                  //Saving the document......................................................
                  doc.Save("C:/Web.config");
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-04-08
            • 1970-01-01
            • 2014-11-28
            • 2017-09-01
            • 1970-01-01
            • 2017-03-28
            • 2018-12-06
            • 1970-01-01
            相关资源
            最近更新 更多