2011-03-16 73 views
2

HI, 我正在使用TFS API。我正在尝试从TFS获取项目,子项目和文件的完整列表。 有人可以指导我关于它。是否有可能使用TFS API获取所有项目和子项目

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

以上代码仅显示TFS的第一级。我如何进一步深入TFS树。 我想要整个项目列表,子项目文件。

感谢, SV

回答

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

用你们&一(谢谢),我能够进行大量的试验和错误后,把这个样品在一起。它进一步展示了如何映射本地路径。我希望这可以节省一些读者头痛。

这个例子放在一起的形式在VS 2015和使用下列程序集引用(即也是棘手的追查)

所有位于C:\ Program Files文件(x86)的\微软的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))); 
      } 
     } 

    } 
} 
相关问题