C# 如何使用 tfs api 列出团队项目的文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/469732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 04:39:11  来源:igfitidea点击:

How to list files of a team project using tfs api?

c#apitfs

提问by Fadeproof

I am wondering if there is a way for me to list all 'files' containted in a tfs team project. What I am aiming to do is search for files of a particular name that dont have fixed paths within TFS caused by branching ($/MyTeamProject/Main/Build/instruction.xml and $/MyTeamProject/Branches/Release_1.0). Once a file would be found I would like to manipulate it.

我想知道是否有办法列出 tfs 团队项目中包含的所有“文件”。我的目标是搜索特定名称的文件,这些文件在 TFS 中没有由分支引起的固定路径($/MyTeamProject/Main/Build/instruction.xml 和 $/MyTeamProject/Branches/Release_1.0)。一旦找到一个文件,我想操纵它。

I guess that we are talking items when it comes to entities containted within a team project and not traditional files and therefore this might be a bit tricky?

我想我们谈论的是团队项目中包含的实体而不是传统文件时的项目,因此这可能有点棘手?

I have seen some samples for manipulating a file but all samples so far have fixed paths.

我看过一些用于操作文件的示例,但到目前为止所有示例都有固定路径。

回答by vengafoo

Here is how I've figured out how to list all the files of a TFS Project:

以下是我想出如何列出 TFS 项目的所有文件的方法:

Add Microsoft.TeamFoundation.Client and Microsoft.TeamFoundation.VersionControl.Client as a reference to your project.

添加 Microsoft.TeamFoundation.Client 和 Microsoft.TeamFoundation.VersionControl.Client 作为对项目的引用。

Add a using for Microsoft.TeamFoundation.Client and Microsoft.TeamFoundation.VersionControl.Client

为 Microsoft.TeamFoundation.Client 和 Microsoft.TeamFoundation.VersionControl.Client 添加一个 using

TeamFoundationServer server = new TeamFoundationServer("server");
VersionControlServer version = server.GetService(typeof(VersionControlServer)) as VersionControlServer;

ItemSet items = version.GetItems(@"$\ProjectName", RecursionType.Full);
ItemSet items = version.GetItems(@"$\ProjectName\FileName.cs", RecursionType.Full);

foreach (Item item in items.Items)
{
    System.Console.WriteLine(item.ServerItem);
}

The second GetItems will restrict the items found to those of a specific filename. I just have this sample outputting the server path for all of the files found.

第二个 GetItems 会将找到的项目限制为具有特定文件名的项目。我只是让这个示例输出找到的所有文件的服务器路径。

回答by dyslexicanaboko

This is not a different answer, but simply an upgrade of Vengafoo's code. The TeamFoundationServer class is obsolete in 2011 (not sure when this happened, I just know it is obsolete as of now). Vengafoo's code is from 2009, so that makes sense. Use the TfsTeamProjectCollection class with the TfsTeamProjectCollectionFactory factory class instead.

这不是一个不同的答案,而只是 Vengafoo 代码的升级。TeamFoundationServer 类在 2011 年已过时(不确定何时发生,我只知道它现在已过时)。Vengafoo 的代码来自 2009 年,所以这是有道理的。将 TfsTeamProjectCollection 类与 TfsTeamProjectCollectionFactory 工厂类一起使用。

Here is the upgrade, just one line of change:

这是升级,只有一行更改:

//TeamFoundationServer server = new TeamFoundationServer("server");
TfsTeamProjectCollection server = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://tfsServerURI:8080/tfs/"));

VersionControlServer version = server.GetService(typeof(VersionControlServer)) as VersionControlServer;

ItemSet items = version.GetItems(@"$\ProjectName", RecursionType.Full);
//ItemSet items = version.GetItems(@"$\ProjectName\FileName.cs", RecursionType.Full);

foreach (Item item in items.Items)
{
    System.Console.WriteLine(item.ServerItem);
}