是否可以使用TFS API获取所有项目和子项目

时间:2011-03-16 09:32:53

标签: tfs c#-4.0

我正在研究TFS API。我想从TFS获取整个项目,子项目,文件列表。 有人可以指导我。

TfsTeamProjectCollection teamProjectCollection = teamFoundationserver.TfsTeamProjectCollection;
ProjectCollection projCollect = (ProjectCollection) teamProjectCollection.GetService(typeof(ProjectCollection));

上面的代码只显示了TFS的第一级。我怎样才能进一步深入TFS树。 我想要整个项目列表,以及每个项目下的解决方案

谢谢, SV

2 个答案:

答案 0 :(得分:7)

没有“子项目”这样的东西。你想要做的是获得每个项目下所有子文件夹/文件的列表。

为此,请遍历每个项目,并对每个项目执行GetItems。这是一些代码:

TfsTeamProjectCollection teamProjectCollection = new TfsTeamProjectCollection(new Uri("http://sw100429:8080"));

ProjectCollection projCollect = (ProjectCollection)teamProjectCollection.GetService(typeof(ProjectCollection));

VersionControlServer vcs = teamProjectCollection.GetService<VersionControlServer>();

// This approach lets you get the list of files for each team project individually.

foreach (TeamProject tp in projCollect)
{
    string path = string.Format("$/{0}", tp.Name);
    var filesAndFolders = vcs.GetItems(path, RecursionType.Full);
}


// However, this approach is a bit more succinct - instead
// of getting them for each team project, just start at "$/" and work your way down

var allFilesAndFolders = vcs.GetItems("$/", RecursionType.Full);

答案 1 :(得分:1)

使用你的q&amp; a(谢谢)我经过大量的试验和错误后能够把这个样本放在一起。它更进一步展示了如何映射本地路径。我希望这可以为一些读者节省一些头痛。

此示例在VS 2015中以表格形式放在一起,并使用以下程序集引用(跟踪也很棘手)

全部位于我机器上的 C:\ Program Files(x86)\ Microsoft Visual Studio 14.0 \ Common7 \ IDE \ Extensions \ vl45o2it.tph 中。

Microsoft.TeamFoundation.Client.dll
Microsoft.TeamFoundation.Common.dll
Microsoft.TeamFoundation.VersionControl.Client.dll
Microsoft.VisualStudio.TeamFoundation.dll

如果我的术语出现在地方,请道歉。如果您编辑其中的任何内容,我不介意。

using System;
using System.Linq;
using System.Windows.Forms;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Common;
using Microsoft.TeamFoundation.Framework.Client;
using System.Diagnostics;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace Tfs
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            Uri tfsUri = new Uri("http://server:8080/tfs");
            string repositoryName = "yourrepository";
            string projectPath = "$/project/path/path/path";

            Uri repositoryUri = new Uri(string.Format("{0}/{1}", tfsUri.AbsoluteUri, repositoryName));

            TfsConfigurationServer tfscs = TfsConfigurationServerFactory.GetConfigurationServer(tfsUri);

            //get the repository
            CatalogNode repository = tfscs.CatalogNode.QueryChildren(new Guid[] { CatalogResourceTypes.ProjectCollection }, false, CatalogQueryOptions.None)
                .FirstOrDefault(a => string.Compare(a.Resource.DisplayName, repositoryName, true) == 0);

            //open in the project collection
            TfsTeamProjectCollection pc = tfscs.GetTeamProjectCollection(new Guid(repository.Resource.Properties["InstanceId"]));

            //tfs project file structure access
            VersionControlServer vcs = pc.GetService<VersionControlServer>();

            WorkspaceInfo wsi = Workstation.Current.GetAllLocalWorkspaceInfo().FirstOrDefault(a => a.ServerUri == repositoryUri);

            //user functionality (checkin, localpaths etc)
            Workspace ws = wsi.GetWorkspace(pc);

            //get the file structure
            ItemSet items = vcs.GetItems(projectPath, RecursionType.Full);

            foreach (Item i in items.Items)
            {
                Debug.WriteLine(string.Format("{0} ({1}) - {2} - {3}", i.ServerItem,
                                                                       i.ContentLength.ToString(),
                                                                       i.ItemType.ToString(),
                                                                       ws.GetLocalItemForServerItem(i.ServerItem)));
            }
        }

    }
}