从 TFS 下载工作项附件(文件已损坏) [英] Download workitem attachments from TFS (files corrupted)

查看:39
本文介绍了从 TFS 下载工作项附件(文件已损坏)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建 C# 代码,因此我可以从 Team Foundation Server 自动下载所有附件以进行 BUGS 的预定义查询.代码似乎工作得很好,但所有下载的文件都出于意外原因损坏了,我无法查看它们.有人可以看看代码并分享意见吗?非常感谢您的帮助!

I am trying to create C# code, so I can automatically download all attachments for predefined query of BUGS from Team Foundation Server. The code seems to work just fine, yet all downloaded files are for an unexpected reason corrupted and I cannot view them. Could someone please take a look at the code and share an opinion? Many thanks for the help!

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

推荐答案

这是由于 WebClient 没有正确验证到 TFS 服务器造成的.您可以使用 Microsoft.TeamFoundation.WorkItemTracking.Proxy 中的 WorkItemServer.DownloadFile() 方法下载文件,而不是使用 WebClient 下载文件.详情请参考以下代码:

This is caused by the WebClient didn't authenticate to TFS server correctly. Instead of using WebClient to download the file, you can use WorkItemServer.DownloadFile() method in Microsoft.TeamFoundation.WorkItemTracking.Proxy to download the file. Refer to the code below for details:

foreach (Attachment atm in workItem.Attachments)
            {
                WorkItemServer wise = tpc.GetService<WorkItemServer>();
                string file = wise.DownloadFile(atm.Id);
                File.Copy(file,"C:\\TEST\\" + atm.Name,true);

            }

这篇关于从 TFS 下载工作项附件(文件已损坏)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆