我正在尝试创建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);
}
}
答案 0 :(得分:0)
对于身份验证,您可以添加以下代码来验证项目集合级别。
//get credentials by logging in with a user interface
var credentialsProvider = new UICredentialsProvider();
var teamProjectCollection = new TfsTeamProjectCollection(collection, credentialsProvider);
teamProjectCollection.Authenticate();
更多详情请参阅此博客:Team Foundation Service: Downloading attachments from work items through the api
答案 1 :(得分:0)
这是由于WebClient未正确向TFS服务器进行身份验证。您可以使用WorkItemServer.DownloadFile()
中的Microsoft.TeamFoundation.WorkItemTracking.Proxy
方法下载文件,而不是使用WebClient下载文件。有关详细信息,请参阅以下代码:
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);
}
答案 2 :(得分:0)
文件未损坏,下载的附件为 zip 格式。您可以检查“损坏的文件”的大小。它的大小会更小。因此,在下载时,您将文件名设为 =====> filename+".zip" ===>当您提取并查看它时,这将为我们提供正确的文件。
示例。
解决方案。