我想使用Microsoft.Build.Evaluation.Project.RemoveItem有计划地删除文件,我已经写了以下几行代码。
var projectFileName = HttpContext.Current.Server.MapPath("~/test.csproj");
var strIncludeFiles = new Microsoft.Build.Evaluation.Project(projectFileName);
var item1 = Helper.GetProjectItem(strIncludeFiles, "\"Compile\"=\"Areas\\TEST\\Models\\Doc1Doc3XRefScriptingModel.cs");
strIncludeFiles.RemoveItem(item1);
strIncludeFiles.Save();
public static ProjectItem GetProjectItem(this Project project, string filePath)
{
var includePath = filePath.Substring(project.DirectoryPath.Length + 1);
var projectItem = project.GetItems("Compile").FirstOrDefault(item => item.EvaluatedInclude.Equals(filePath));
return projectItem;
}但在item1中,我总是得到null value.So,请告诉我我哪里错了,以及如何删除文件。
发布于 2017-12-18 12:43:00
与Microsoft.Build.Evaluation.Project.RemoveItem.无关,item1为null这是GetProjectItem方法的FirstOrDefault中的一个问题。如果您遍历项目中的项,您将看到EvaluatedInclude格式。没有"Compile"=前缀属性,EvaluatedInclude。只需指定与EvaluatedInclude匹配的文件夹路径即可。
var item1 = Helper.GetProjectItem(strIncludeFiles,
"Areas\\TEST\\Models\\Doc1Doc3XRefScriptingModel.cs");https://stackoverflow.com/questions/43730663
复制相似问题