c#包含类文件到csprj中

  public ActionResult Index()
        {

            XDocument doc = XDocument.Load(@"G:\users\kim.gao\documents\visual studio 2015\Projects\test\test\test.csproj");
            ContainFileToCsproj(@"T4\AccountViewModels.cs", "", "", doc);
            doc.Save(@"G:\users\kim.gao\documents\visual studio 2015\Projects\test\test\test.csproj");
            return View();
        }
        public static void ContainFileToCsproj(string fileName, string solutionDir, string csprojName, XDocument doc)
        {
            string path = @"G:\users\kim.gao\documents\visual studio 2015\Projects\test\test\test.csproj";
            XElement root = doc.Root;
            string xmlns = "{" + root.Attribute("xmlns").Value + "}";
            IEnumerable<XElement> compileList = root.Elements(xmlns + "ItemGroup").Elements(xmlns + "Compile");
            XElement itemGroup = compileList.FirstOrDefault().Parent;
            if (compileList.Where(u => u.Attribute("Include").Value == fileName).Count() > 0)
            {
                compileList.Where(u => u.Attribute("Include").Value == fileName).Remove();
            }

            XElement compile = new XElement(xmlns + "Compile");
            compile.SetAttributeValue("Include", fileName);
            itemGroup.AddFirst(compile);
        }

 

posted @ 2017-08-16 21:26  小小高  阅读(363)  评论(0编辑  收藏  举报