2011-03-16 109 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))); 
      } 
     } 

    } 
} 
相關問題