c#包含类文件到csprj中

  public ActionResult Index()
        {

            XDocument doc = XDocument.Load(@"G:userskim.gaodocumentsvisual studio 2015Projects	est	est	est.csproj");
            ContainFileToCsproj(@"T4AccountViewModels.cs", "", "", doc);
            doc.Save(@"G:userskim.gaodocumentsvisual studio 2015Projects	est	est	est.csproj");
            return View();
        }
        public static void ContainFileToCsproj(string fileName, string solutionDir, string csprojName, XDocument doc)
        {
            string path = @"G:userskim.gaodocumentsvisual studio 2015Projects	est	est	est.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);
        }
原文地址:https://www.cnblogs.com/gaocong/p/7376022.html