0
我想通過在web config中添加一個新行來修改特定文件夾中所有項目的Web配置文件?有什麼建議如何實現這一目標?修改多個項目的webconfig文件
我想通過在web config中添加一個新行來修改特定文件夾中所有項目的Web配置文件?有什麼建議如何實現這一目標?修改多個項目的webconfig文件
一個小控制檯應用程序,有這樣的事情:
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);
}
感謝您的回答,怎麼樣使用控制檯應用程序在多個asp.net mvc項目中添加程序集引用? – 2012-07-30 05:40:53
@ManickamChidhambaram請參閱編輯。順便說一下,你可以嘗試... – 2012-07-30 07:01:50
感謝您的回答。它工作正常。 – 2012-07-30 11:06:21