我試圖創建C#代碼,因此我可以自動從Team Foundation Server中下載所有BUG預定義查詢的附件。該代碼似乎工作得很好,但所有下載的文件出於意外的原因損壞,我無法查看它們。有人可以看看代碼並分享意見嗎?非常感謝您的幫助!從TFS下載工作項附件(損壞的文件)
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時,我需要使用電話身份驗證+憑證。然而,它很奇怪,因爲代碼能夠獲得所有的附件,但是以損壞的形式。我還發現了以下文章,其中的代碼使用了令牌認證。但它是相反的過程(附件上傳/不下載)。非常感謝您的幫助。 –
試試這個博客的方法http://www.timschaeps.com/team-foundation-service-downloading-attachments-from-work-items-through-the-api/ –