从TFS以ZIP格式通过C#下载文件

时间:2018-06-28 14:52:43

标签: c# tfs

我想使用TeamFoundation .NET库从TFS下载文件夹。我可以单独下载文件,但是花费的时间太长。有没有办法将整个目录下载为ZIP或类似格式?

这是我当前正在使用的代码。

        var client = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(GetToken));

        //Connect to TFS server.
        var tfs = new TfsConfigurationServer(new Uri("tfs server url"), new NetworkCredential("username", "password", "domain"));
        tfs.EnsureAuthenticated();

        //Get the default collection id.
        var collectionId = new Guid(tfs.CatalogNode.QueryChildren(new[] { CatalogResourceTypes.ProjectCollection }, false, CatalogQueryOptions.None).First().Resource.Properties["InstanceId"]);

        //Get the default collection.
        var collection = tfs.GetTeamProjectCollection(collectionId);

        //Download files.
        var server = collection.GetService<VersionControlServer>();
        var items = server.GetItems(application.branch_directory + "/" + environment.name, VersionSpec.Latest, RecursionType.Full, DeletedState.NonDeleted, ItemType.Any, true).Items;
        foreach (var item in items)
        {
            var target = Path.Combine(Path.Combine(applicationDirectory, "$"), item.ServerItem.Substring(2));
            if (item.ItemType == ItemType.Folder && !Directory.Exists(target))
            {
                Directory.CreateDirectory(target);
            }
            else if (item.ItemType == ItemType.File)
            {
                item.DownloadFile(target);
            }
        }

3 个答案:

答案 0 :(得分:1)

您可以使用Rest API--Get a specific version来执行此操作,并使用httpclient调用Rest API。

您可以在获取文件,压缩文件夹或获取项目元数据时指定要获取的版本。

  

此格式应用于某些文件(如web.config),由于默认的ASP .NET保护,使用路径作为URL的一部分无法访问这些文件(如web.config)。响应是一个包含文件内容的流(application / octet-stream)。

http://fabrikam-fiber-inc.visualstudio.com/defaultcollection/_apis/tfvc/items?path=$/fabrikam-fiber-tfvc/website/website/web.config&api-version={version}

使用路径作为查询参数时需要压缩文件。

要获取整个文件夹,您可以将其压缩并与Rest Api以以下格式下载:

[Get] https://xxx/defaultcollection/_apis/tfvc/items?path=<Folder Path>&api-version=1.0

并在请求标头中添加以下内容:Accept: application/zip

供参考的代码示例:How to stream zip file from TFS api

答案 1 :(得分:0)

我找到了使用TeamFoundation .NET库的解决方案。

        //Connect to TFS server.
        var tfs = new TfsConfigurationServer(new Uri("tfs server"), new VssCredentials(new Microsoft.VisualStudio.Services.Common.WindowsCredential(new NetworkCredential("username", "password", "domain"))));
        tfs.EnsureAuthenticated();

        //Get the default collection id.
        var collectionId = new Guid(tfs.CatalogNode.QueryChildren(new[] { CatalogResourceTypes.ProjectCollection }, false, CatalogQueryOptions.None).First().Resource.Properties["InstanceId"]);

        //Get the default collection.
        var collection = tfs.GetTeamProjectCollection(collectionId);

        //Download files.
        var server = collection.GetClient<TfvcHttpClient>();
        var zip = Path.GetTempFileName();
        var stream = File.Create(zip);
        var item = server.GetItemZipAsync(application.branch_directory + "/" + environment.name).Result;
        item.CopyTo(stream);
        stream.Close();
        try
        {
            ZipFile.ExtractToDirectory(zip, applicationDirectory);
        }
        catch
        {
            throw new Exception("Unable to unzip the file: " + zip);
        }

答案 2 :(得分:0)

@kernowcode这些对我有用:

modA

但这是简单的部分。查找DLL的/包是另一回事。我使用了这些,但是您的计算机上的版本/路径可能不同

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.Framework.Common;
using Microsoft.TeamFoundation.SourceControl.WebApi;
相关问题