简体   繁体   English

如何使用 Microsoft.Build.Evaluation.Project.RemoveItem

[英]How to use Microsoft.Build.Evaluation.Project.RemoveItem

I've successfuly added files programatically to my project using the following code:我已经成功地使用以下代码以编程方式将文件添加到我的项目中:

var project = new Microsoft.Build.Evaluation.Project(projPath);
project.AddItem("Compile", filePath);

However, removing a file programatically is giving me a hard time.但是,以编程方式删除文件给我带来了困难。

Signature:签名:

public bool RemoveItem(
    ProjectItem item
)

How can I instantiate a ProjectItem ?如何实例化ProjectItem I couldn't find any examples.我找不到任何例子。

Reference: https://msdn.microsoft.com/en-us/library/microsoft.build.evaluation.project.removeitem.aspx参考: https : //msdn.microsoft.com/en-us/library/microsoft.build.evaluation.project.removeitem.aspx

you did你做到了

      private static ProjectItem GetProjectItem(this Project project, string filePath)
    {
        var includePath = filePath.Substring(project.DirectoryPath.Length + 1);
        var projectItem = project.GetItems(CompileType).FirstOrDefault(item => item.EvaluatedInclude.Equals(includePath));
        return projectItem;
    }

in your GetProjectItem method:在您的GetProjectItem方法中:

replace that:替换那个:

var projectItem = project.GetItems(CompileType).FirstOrDefault(item => item.EvaluatedInclude.Equals(includePath));

with this:有了这个:

       var projectItem = project.GetItems("Compile").ToList()
            .Where(item => item.EvaluatedInclude.Equals(includePath)).FirstOrDefault();

using .FirstOrDefault() will bring it to have just first item of all files.使用.FirstOrDefault()将使它具有所有文件的第一项。 i used .ToList() and made it work with all my items which have same EvaluatedInclude .我使用了.ToList()并使其适用于我所有具有相同EvaluatedInclude 的项目。 its totally worked for my.它完全为我工作。

This is the class I ended up writing.这是我最后写的课。 No simple solution for remove.没有简单的删除解决方案。

    public static class SourceControlHelper
    {
        public static void CheckoutFile(string filePath)
        {
            TFSAction((workspace) => workspace.PendEdit(filePath), filePath);
        }

        public static void AddFile(this Project project, string filePath)
        {
            CheckoutFile(project.FullPath);
            var projectItem = project.GetProjectItem(filePath);
            if (projectItem != null)
            {
                return;
            }
            var includePath = filePath.Substring(project.DirectoryPath.Length + 1);
            project.AddItem(CompileType, includePath);
            project.Save();
            TFSAction(workspace => workspace.PendAdd(filePath), filePath);
        }

        public static void DeleteFile(this Project project, string filePath)
        {
            CheckoutFile(project.FullPath);
            var projectItem = project.GetProjectItem(filePath);
            if (projectItem == null)
            {
                return;
            }
            project.RemoveItem(projectItem);
            project.Save();
            TFSAction(workspace => workspace.PendDelete(filePath), filePath);
        }

        private static ProjectItem GetProjectItem(this Project project, string filePath)
        {
            var includePath = filePath.Substring(project.DirectoryPath.Length + 1);
            var projectItem = project.GetItems(CompileType).FirstOrDefault(item => item.EvaluatedInclude.Equals(includePath));
            return projectItem;
        }

        private static void TFSAction(Action<Workspace> action, string filePath)
        {
            var workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(filePath);
            if (workspaceInfo == null)
            {
                Console.WriteLine("Failed to initialize workspace info");
                return;
            }
            using (var server = new TfsTeamProjectCollection(workspaceInfo.ServerUri))
            {
                var workspace = workspaceInfo.GetWorkspace(server);
                action(workspace);
            }
        }

        private static string CompileType
        {
            get { return CopyTool.Extension.Equals("ts") ? "TypeScriptCompile" : "Compile"; }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Microsoft.Build.Evaluation.Project.RemoveItem删除的项目 - Removed item using Microsoft.Build.Evaluation.Project.RemoveItem 如何在MSBuild 14.0中重新加载Microsoft.Build.Evaluation.Project - How can I reload a Microsoft.Build.Evaluation.Project in MSBuild 14.0 使用Microsoft.Build.Evaluation发布数据库项目(.sqlproj) - Using Microsoft.Build.Evaluation to publish a database project (.sqlproj) 无法在 VS2017 中使用 Microsoft.Build.Evaluation - Can't use Microsoft.Build.Evaluation in VS2017 从C#运行Microsoft.Build.Evaluation.Project的并行构建 - Run parallel build of Microsoft.Build.Evaluation.Project from C# 弄清楚为什么 Microsoft.Build.Evaluation.Project.Build() 返回 false - Figure out why Microsoft.Build.Evaluation.Project.Build() returns false Microsoft.Build.Evaluation.Project 在VS中添加文件夹和文件强制刷新项目 - Microsoft.Build.Evaluation.Project add folder and file force refresh of project in VS 如何使用 microsoft.build.evaluation 以编程方式添加新的 dll 参考 - How to add new dll reference programmatically using microsoft.build.evaluation 如何使用 Microsoft.Build.CentralPackageVersions? - How to use Microsoft.Build.CentralPackageVersions? 使用 Microsoft.Build.Evaluation(而不是 Engine)以编程方式修改 csproj 文件 - Modify programatically csproj files with Microsoft.Build.Evaluation (instead of Engine)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM