从 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:tempvsts(

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

最新更新