【发布时间】:2021-05-28 14:19:20
【问题描述】:
我正在尝试创建 C# 代码,因此我可以从 Team Foundation Server 自动下载所有附件以进行预定义的 BUGS 查询。该代码似乎运行良好,但所有下载的文件都因意外原因损坏,我无法查看它们。有人可以看看代码并分享意见吗?非常感谢您的帮助!
static void Main()
{
// Connection to the Team Project Collection
TfsTeamProjectCollection tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(
new Uri("https://xxx.visualstudio.com/defaultcollection"));
// Get a WorkItemStore object for the Team Project Collection.
WorkItemStore workItemStore = new WorkItemStore(tpc);
// Run a query.
WorkItemCollection queryResults = workItemStore.Query(
@"SELECT *
FROM WorkItems
WHERE [System.WorkItemType] = 'Bug'
AND [Language] = 'xxx'
AND [How Found] = 'xxx'
AND [Assigned to] = 'xxx'
ORDER BY [Changed Date] DESC");
// Get a WebClient object to do the attachment download
WebClient webClient = new WebClient()
{
UseDefaultCredentials = true
};
// Loop through each work item.
foreach (WorkItem workItem in queryResults)
{
// Loop through each attachment in the work item.
foreach (Attachment attachment in workItem.Attachments)
{
// Construct a filename for the attachment
string filename = string.Format("C:\\TEST\\{0}_{1}", workItem.Fields["ID"].Value, attachment.Name);
// Download the attachment.
webClient.DownloadFile(attachment.Uri, filename);
}
}
【问题讨论】:
-
这个错误只发生在你的开发机器上吗?你能在另一台机器上试一试吗?
-
嗨。是的,我同事机器上的错误也完全相同。我想知道这是否可能是身份验证问题。正常登录 VSTS 时,我需要使用电话身份验证 + 凭据。然而奇怪的是,代码能够获取所有附件,但形式已损坏。我还找到了以下文章,其中代码使用令牌身份验证。但这是针对相反的过程(附件上传/不下载)。非常感谢您的帮助。
-
试试这个博客里的方法timschaeps.com/…