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