2013-07-19 41 views
5

我需要找出項目映射到本地或不從代碼。我可以使用Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory.GetTeamProjectCollection()獲得所有TFS項目,比我可以做foreach獲得workItemStore = new WorkItemStore(projects),並獲得大量項目信息,但是像IsMappedMappingPath程序化檢查如何在TFS中映射項目?

我需要的信息可以通過Visual Studio中的團隊資源管理器的源代碼管理資源管理器輕鬆訪問,但我需要從C#代碼中完成。

這就是我想:

​​

UPD:ANSWER

MikeR的回答是不錯的,但我想補充一點,它有一個缺陷。如果您的根目錄已映射,但實際上並未在本地計算機上擁有此根目錄下的所有項目,則Miker的解決方案仍將返回全部項目。如果你不想讓你的代碼,以這樣的行爲,這是我的解決方案:

TfsTeamProjectCollection teamProjectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(_tfsServerUri)); 
teamProjectCollection.Authenticate(); 
VersionControlServer versionControl = teamProjectCollection.GetService<VersionControlServer>(); 

string computerName = Environment.MachineName; 
WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent(); 
// get yours local workspaces 
Workspace[] workspaces = versionControl.QueryWorkspaces(null, windowsIdentity.Name, computerName); 

foreach (Project pr in workItemStore.Projects) 
    { 
     var mapped = false; 

     foreach (Workspace workspace in workspaces) 
     { 
      var path = workspace.TryGetLocalItemForServerItem("$/" + pr.Name); 
      if (!String.IsNullOrEmpty(path) && Directory.Exists(path)) 
      { 
       mapped = true; 
      } 
     } 
    // do what you want with mapped project 
    } 

回答

3

這更多的是一種通用的方法,但我想你會管理,即可定製適合您的需求(編譯沒有,只是指出到方向):

string project = "TeamProject1"; 
string serverPath = "$/"+project; 
string computername = "myComputer"; // possibly Environment.Computer or something like that 
var tpc= TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(_tfsServerUri)); 
tpc.Authenticate(); 
// connect to VersionControl 
VersionControlServer sourceControl = (VersionControlServer)tpc.GetService(typeof(VersionControlServer)); 
// iterate the local workspaces 
foreach (Workspace workspace in sourceControl.QueryWorkspaces(null, null, computername)) 
{ 
    // check mapped folders 
    foreach (WorkingFolder folder in workspace.Folders) 
    { 
    // e.g. $/TeamProject1 contains $/ if the root is mapped to local 
    if (serverPath.Contains(folder.ServerItem) && !folder.IsCloaked) 
    { 
     Console.WriteLine(serverPath + " is mapped under "+ folder.LocalItem); 
     Console.WriteLine("Workspacename: "+workspace.Name); 
    } 
    } 
}