使用批处理文件或.NET代码更改Web.config中的值

4
我在我的电脑上有一个web.config文件。
有许多东西我需要在文件中更改和添加。 (实际上,我正在处理我的SharePoint web.config文件)
如果可以的话,我是否可以使用批处理文件来完成这个操作?如果可以,应该如何操作? 或者应该如何使用VB.NET或C#代码完成它?
大家有什么想法吗?
编辑:我需要创建一个程序来修改一个Web.config文件,例如我桌面上的Web.config文件,而不是我的项目的实际Web.config文件。
问候 艾蒂安 (Etienne)
7个回答

4
您可以从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)方法。

有关OpenWebConfiguration方法和更多信息,请参见msdn文档


谢谢,但我需要创建一个程序来修改一个位于我的桌面上的web.config文件,而不是实际项目中的web.config文件....我该怎么做? - Etienne

3
您想要更改文件的上下文确实会影响您应该如何进行更改。如果您希望经常执行更改,但在管理域中,则某种命令行工具是有意义的,在这种情况下,我同意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。 - Etienne
PowerShell和LINQ仍然完全能够胜任此任务。 - Greg D
你好,我现在已经查看了。我必须使用LINQ打开web.config吗?如果是这样,请给我一个例子,告诉我如何打开位于C:\MyFile\web.config上的配置文件。 - Etienne

1

加载任意的 .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”? - Etienne

1

1

我个人建议使用PowerShell。这是微软的下一代命令行,紧贴着 .Net。它被设计用于在大量文件之间进行批量编辑等操作。


1
在SharePoint环境中更改web.config,您需要专门为此任务开发的类。您只需搜索SPWebConfigModification类即可。

0

这就是我需要做的……谢谢你们的帮助!!!

// 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");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接