如何以编程方式检查TFS中的项目映射?

时间:2013-07-19 08:39:39

标签: c# tfs

我需要找出是在本地映射的项目还是不从代码中映射的项目。我可以使用Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory.GetTeamProjectCollection()获取所有TFS项目,而foreach可以workItemStore = new WorkItemStore(projects)获取所有TFS项目,并获取大量项目信息,但IsMappedMappingPath等。

我需要的信息可以从Visual Studio中的团队资源管理器的源代码管理资源管理器中轻松访问,但我需要从C#代码中进行。

这就是我的尝试:

var projects = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(_tfsServerUri));
projects.Authenticate();
var workItemStore = new WorkItemStore(projects);
foreach (Project pr in workItemStore.Projects)
    {
        pr.IsLocal;
    }

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 
    }

1 个答案:

答案 0 :(得分: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);
     }
  }
}