Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 修改多个项目的webconfig文件_Asp.net Mvc - Fatal编程技术网

Asp.net mvc 修改多个项目的webconfig文件

Asp.net mvc 修改多个项目的webconfig文件,asp.net-mvc,Asp.net Mvc,我想通过在web配置中添加新行来修改特定文件夹中所有项目的web配置文件?有人能建议如何实现这一点吗?一个小型控制台应用程序,具有以下功能: var path = <YourDirectory>; var newValue = <a new Value in the new line>; var files = Directory.GetFiles(path, "web.config", SearchOption.AllDirectories); foreach (va

我想通过在web配置中添加新行来修改特定文件夹中所有项目的web配置文件?有人能建议如何实现这一点吗?

一个小型控制台应用程序,具有以下功能:

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);
}

感谢您的回答,如何使用控制台应用程序在多个asp.net mvc项目中添加程序集引用?对于修改项目文件以添加程序集引用,此代码被截断,无法工作。还有别的办法吗?
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);
 }