如何使用 TfvcHttpClient 从 Azure DevOps 下载文件

问题描述 投票:0回答:1

我想使用 TfvcHttpClient 类从我的 Azure Dev Ops 代码存储库目录之一下载所有文件的最新版本。不幸的是,这没有很好的记录,我也找不到有用的示例程序。

到目前为止我所得到的是:

var creds = new VssBasicCredential(string.Empty, "PAT_GOES_HERE");
var connection = new VssConnection(new  Uri("https://username.visualstudio.com/DefaultCollection"), creds);
using var tfsClient = connection.GetClient<TfvcHttpClient>();

var result = tfsClient.GetItemsBatchZipAsync(new TfvcItemRequestData() {ItemDescriptors = new []{new TfvcItemDescriptor(){Path="/", RecursionLevel = VersionControlRecursionType.Full,VersionType = TfvcVersionType.Latest}} }, "Testcases").Result;
var fs = File.Create("c:\\temp\\x.zip");
result.CopyTo(fs);

创建的文件大小为 0,但没有复制任何字节,并且 CopyTo() 不返回。

如何下载最新版本的目录?

c# azure-devops tfs azure-devops-services .net-sdk
1个回答
0
投票

这是一个适用于当前版本的 .net 库 (16.205.1) 的完整解决方案。

using System.IO.Compression;
using Microsoft.TeamFoundation.SourceControl.WebApi;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi;

namespace GetCurrent
{
    internal class Program
    {
        private static readonly string DevOpsPat = Environment.GetEnvironmentVariable("DIE_DEVOPS_PAT") ?? throw new InvalidOperationException("DIE_DEVOPS_PAT");
        private const string DestinationDirectory = "c:\\temp\\testcases";

        static void Main()
        {
            var credentials = new VssBasicCredential(string.Empty, DevOpsPat);
            var connection = new VssConnection(new Uri("https://twoelfer.visualstudio.com/DefaultCollection"), credentials);

            using var tfsClient = connection.GetClient<TfvcHttpClient>();

            var zip = Path.GetTempFileName();
            using var stream = File.Create(zip);
            using var item = tfsClient.GetItemZipAsync( "$/Testcases" ).Result;
            item.CopyTo(stream);
            stream.Close();
            try
            {
                ZipFile.ExtractToDirectory(zip, DestinationDirectory);
                File.Delete(zip);
            }
            catch
            {
                throw new Exception("Unable to unzip the file: " + zip);
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.