Team Foundation Server TFS的API删除问题

时间:2018-04-02 15:34:19

标签: api tfs

您好,有人能告诉我如何使用TFS API删除文件吗?以下是我所拥有的,但我无法让它工作任何帮助真的很感激。

string[] InLocalDirectory = Directory.GetFiles(LogicAppConfig.Query(AppConfigLogic.TypeOfConfig.Path), "*", SearchOption.AllDirectories);

// Source Control
List<string> InSourceControl = new List<string>();
ItemSet SetOfItem = _ServerVersionControl.GetItems(_ServerPath, VersionSpec.Latest, RecursionType.Full);
foreach (Item GotItem in SetOfItem.Items)
{
    ItemType TypeOfItem = GotItem.ItemType;
    if (TypeOfItem == ItemType.File)
    {
        string LocalPath = _WorkspaceLocal.GetLocalItemForServerItem(GotItem.ServerItem);
        InSourceControl.Add(LocalPath);
    }
}

List<int> ToDeleteById = new List<int>();
foreach (string SourceFile in InSourceControl)
{
    if (!IsIgnored(SourceFile) && !InLocalDirectory.Contains(SourceFile))
    {
        // Delete Source Control File
        Item DeleteItem = _ServerVersionControl.GetItem(SourceFile);
        ToDeleteById.Add(DeleteItem.ItemId);
        // Update Local XML Directory
        DataXml.Delete(SourceFile);
    }
}

WorkItemStore wis = _CollectionTeamProject.GetService<WorkItemStore>();
wis.DestroyWorkItems(ToDeleteById);

3 个答案:

答案 0 :(得分:0)

要删除文件,您需要使用VersionControlServer class来获取现有工作区或创建新工作区。工作空间具有PendDelete方法,用于在工作空间中创建挂起的更改。然后使用Workspace.Checkin方法将它们提交给源代码控制:

https://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.versioncontrol.client.workspace.aspx

答案 1 :(得分:0)

在我尝试最后一种方式之前,我尝试过penddelete。但即使我手动去删除文件,它也会命中_WorkspaceLocal.PendDelete(SourceFile);如果(changes.Count()&gt; 0),则行插入并删除它不会在行上拾取然后再登记。

string[] InLocalDirectory = Directory.GetFiles(LogicAppConfig.Query(AppConfigLogic.TypeOfConfig.Path), "*", SearchOption.AllDirectories);

            // Source Control
            List<string> InSourceControl = new List<string>();
            ItemSet SetOfItem = _ServerVersionControl.GetItems(_ServerPath, VersionSpec.Latest, RecursionType.Full);
            foreach (Item GotItem in SetOfItem.Items)
            {
                ItemType TypeOfItem = GotItem.ItemType;
                if (TypeOfItem == ItemType.File)
                {
                    string LocalPath = _WorkspaceLocal.GetLocalItemForServerItem(GotItem.ServerItem);
                    InSourceControl.Add(LocalPath);
                }
            }

            List<int> ToDeleteById = new List<int>();
            foreach (string SourceFile in InSourceControl)
            {
                if (!IsIgnored(SourceFile) && !InLocalDirectory.Contains(SourceFile))
                {
                    // Delete Source Control File
                    Item DeleteItem = _ServerVersionControl.GetItem(SourceFile);
                    ToDeleteById.Add(DeleteItem.ItemId);
                    // Update Local XML Directory
                    DataXml.Delete(SourceFile);
                    // Set for Deletion
                    _WorkspaceLocal.PendDelete(SourceFile);
                }
            }

            string ConflictMessage = "";
            Conflict[] conflicts = _WorkspaceLocal.QueryConflicts(new string[] { _LocalPath }, true);
            foreach (Conflict conflict in conflicts)
            {
                if (conflict != null)
                {
                    try
                    {
                        if (conflict.CanMergeContent)
                        {
                            conflict.Resolution = Resolution.AcceptMerge;
                        }
                        else
                        {
                            conflict.Resolution = Resolution.AcceptYoursRenameTheirs;
                        }
                        ConflictMessage += @"\n\r\n\r" + conflict.GetFullMessage();
                        _WorkspaceLocal.ResolveConflict(conflict);
                    }
                    catch (Exception ex)
                    {
                        LogicAppConfig.Insert(AppConfigLogic.TypeOfConfig.Message, "Error Detected Previously:\r\n\r\n" + ex.Message + "\r\n\r\n" + ex.Source + "\r\n\r\n" + ex.StackTrace + "\r\n\r\n" + LogicAppConfig.Query(AppConfigLogic.TypeOfConfig.Path));
                    }
                }
            }

            if (!String.IsNullOrEmpty(ConflictMessage))
            {
                LogicAppConfig.Insert(AppConfigLogic.TypeOfConfig.Message, ConflictMessage);
            }

            PendingChange[] changes = _WorkspaceLocal.GetPendingChanges();

            if (changes.Count() > 0)
            {
                int ChangeSetId = _WorkspaceLocal.CheckIn(changes, _WorkspaceName + " Deleted by Member Collaboration Utility");
            }

答案 2 :(得分:0)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.Client;
using Microsoft.TeamFoundation.SourceControl.WebApi;
using Microsoft.VisualStudio.Services.WebApi;

namespace ConsoleAppX
{
    class Program
    {
        static void Main(string[] args)
        {
            VssCredentials creds = new VssClientCredentials();
            creds.Storage = new VssClientCredentialStorage();
            VssConnection connection = new VssConnection(new Uri("https://tfsuri"), creds);
            TfvcHttpClient tfvcClient = connection.GetClient<TfvcHttpClient>();
            TfvcItem ti = tfvcClient.GetItemAsync("ProjectName", "$/FilePath","FileName").Result;
            TfvcChange tchange = new TfvcChange(ti,VersionControlChangeType.Delete);          
            List<TfvcChange> change = new List<TfvcChange> { tchange };
            TfvcChangeset tchangeset = new TfvcChangeset();
            tchangeset.Changes = change;
            tfvcClient.CreateChangesetAsync(tchangeset);
        }
    }
}