我想通过在 web config 中添加新行来修改特定文件夹中所有项目的 web config 文件?任何人都可以建议如何实现这一目标?
问问题
100 次
1 回答
0
一个小型控制台应用程序,类似这样:
var path = <YourDirectory>;
var newValue = <a new Value in the new line>;
var files = Directory.GetFiles(path, "web.config", SearchOption.AllDirectories);
foreach (var file in files)
{
var doc = XDocument.Load(file);
var parentElement = doc.Element("blabla");//get the right parent Node
parentElement.Add(new XElement("new name"), newValue);//well put what you want in it : attributes, values...
doc.Save(file);
}
编辑
对于您的情况,只需将其放在主要位置
var path = @"C:\Tools";
var newAssembly = @"System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35";
var files = Directory.GetFiles(path, "Web.config", SearchOption.AllDirectories);
foreach (var file in files)
{
var doc = XDocument.Load(file);
var parentElement = doc.Descendants("assemblies").FirstOrDefault();
if (parentElement != null)
{
var newNode = new XElement("add", new XAttribute("assembly", newAssembly));
parentElement.Add(newNode);
}
doc.Save(file);
}
于 2012-07-27T14:45:50.650 回答