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

时间:2018-10-01 11:55:54

标签: c# tfs tfs-sdk

如何使用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 )
        {

        }
    }

1 个答案:

答案 0 :(得分:0)

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();
        }
    }
}