gpt4 book ai didi

c# - 如何在 Azure DevOps 中以编程方式下载附件?

转载 作者:太空狗 更新时间:2023-10-30 01:13:30 24 4
gpt4 key购买 nike

我正在尝试使用从 Nuget 下载的 TeamFoundation/VisualStudio C# API,以编程方式从 VSTS 中的用户故事下载 90 多个附件。我试图使用这个例子:https://intellitect.com/downloading-attachments-from-tfs/

但是,该代码似乎已过时。我似乎找不到文章中提到的这些确切的包:nuget-bot.Microsoft.TeamFoundation.Clientnuget-bot.Microsoft.TeamFoundation.WorkItemTracking.Client

但是,我下载了那里的 TFS 包,例如 Microsoft.TeamFoundationServer.Client 和 Microsoft.TeamFoundationServer.ExtendedClient,但 WorkItem 类似乎不再有附件。有人知道我在哪里可以找到 Attachments 属性吗?我在 Visual Studio 中浏览了对象浏览器,但找不到它。或者您可以建议一个替代解决方案来从工作项中获取附件吗?谢谢。

最佳答案

WebClient cannot authenticate to VSTS correctly. Instead of using WebClient to download the file, you can use WorkItemServer.DownloadFile()method in Microsoft.TeamFoundation.WorkItemTracking.Proxy to download the file. See this thread for details.

您可以使用以下示例下载特定工作项的附件:

注意:安装nuget包Microsoft.TeamFoundationServer.ExtendedClient

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

namespace VSTS_DownloadWITAttachment
{
class Program
{
static void Main(string[] args)
{
TfsTeamProjectCollection ttpc = new TfsTeamProjectCollection(new Uri("https://account.visualstudio.com/"));
ttpc.EnsureAuthenticated();
WorkItemStore wistore = ttpc.GetService<WorkItemStore>();
WorkItem wi = wistore.GetWorkItem(94);
WorkItemServer wiserver = ttpc.GetService<WorkItemServer>();
string tmppath = wiserver.DownloadFile(wi.Attachments[0].Id); //Change the number to download other attachments if there are more then one attachments for the specific work item. e.g: [1] to download the second one.
string filename = string.Format("D:\\WITAttachments\\{0}-{1}", wi.Fields["ID"].Value, wi.Attachments[0].Name);
File.Copy(tmppath, filename);
}
}
}

然后您可以尝试查询工作项并为每个工作项循环下载附件。参见 Fetch work items with queries programatically in VSTS了解详情。

以下示例供您引用:

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Proxy;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models;
using Microsoft.VisualStudio.Services.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;

namespace DownloadWITAttachments
{
class Program
{
static void Main(string[] args)
{
Uri uri = new Uri("https://account.visualstudio.com");
string PAT = "xxxxxxxxxxxx";
string project = "ProjectName";

VssBasicCredential credentials = new VssBasicCredential("", PAT);

//create a wiql object and build our query
Wiql wiql = new Wiql()
{
Query = "Select * " +
"From WorkItems " +
"Where [Work Item Type] = 'User Story' " +
"And [System.TeamProject] = '" + project + "' " +
"And [System.State] <> 'Closed' " +
"And [System.AttachedFileCount] > 0 " +
"Order By [State] Asc, [Changed Date] Desc"
};

//create instance of work item tracking http client
using (WorkItemTrackingHttpClient workItemTrackingHttpClient = new WorkItemTrackingHttpClient(uri, credentials))
{
//execute the query to get the list of work items in the results
WorkItemQueryResult workItemQueryResult = workItemTrackingHttpClient.QueryByWiqlAsync(wiql).Result;

if (workItemQueryResult.WorkItems.Count() != 0)
{
//Download the first attachment for each work item.
foreach (var item in workItemQueryResult.WorkItems)
{
TfsTeamProjectCollection ttpc = new TfsTeamProjectCollection(uri);
ttpc.EnsureAuthenticated();
WorkItemStore wistore = ttpc.GetService<WorkItemStore>();
Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem wi = wistore.GetWorkItem(item.Id);
WorkItemServer wiserver = ttpc.GetService<WorkItemServer>();
string tmppath = wiserver.DownloadFile(wi.Attachments[0].Id);
string filename = string.Format("D:\\temp\\vsts\\{0}-{1}", wi.Fields["ID"].Value, wi.Attachments[0].Name);
File.Copy(tmppath, filename);
}
}
}
}
}
}

关于c# - 如何在 Azure DevOps 中以编程方式下载附件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50119480/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com