从 TFS 中的共享步骤获取附件

Get attachments from Shared steps in TFS

如何使用 C# 从 TFS 下载共享步骤中的附件。 这是我编写的部分代码:

foreach (WorkItem item in witCollection) //witCollection is collection of shared steps.
{
     if(item.Attachments.Count > 0){          
         AttachmentCollection atcoll =((Microsoft.TeamFoundation.WorkItemTracking.Client.AttachmentCollection)(item.Attachments)) as AttachmentCollection;

        foreach (var itemat in atcoll )
        {

        }
    }

Shared Steps 是工作项类型之一,因此您可以从 WorkItemServer 下载附件。

您可以使用以下 C# 代码示例在共享步骤中下载附件:(在此示例中将附件下载到 D:\temp\vsts

using System;
using Microsoft.VisualStudio.Services.Client;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Proxy;
using System.IO;

namespace RetrieveAttachments
{
    class Program
    {
        static void Main(string[] args)
        {
            var u = new Uri("http://172.17.16.163:8080/tfs/DefaultCollection");
            var c = new VssClientCredentials();
            int SharedStepsID = 748;
            TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(u, c);
            tpc.EnsureAuthenticated();
            WorkItemStore wistore = tpc.GetService<WorkItemStore>();
            WorkItem wi = wistore.GetWorkItem(SharedStepsID);
            WorkItemServer wiserver = tpc.GetService<WorkItemServer>();
            int atc = wi.Attachments.Count;

                for (int i = 0; i < atc; i++)
                {
                    string tmppath = wiserver.DownloadFile(wi.Attachments[i].Id);
                    string filename = string.Format("D:\temp\vsts\{0}-{1}", wi.Fields["ID"].Value, wi.Attachments[i].Name);
                    File.Copy(tmppath, filename);
                    Console.WriteLine(string.Format("{0}\n", filename));
               }      
            Console.ReadLine();
        }
    }
}