TFS中项目映射的编程检查方式

本文关键字:编程 检查 方式 映射 项目 TFS | 更新日期: 2023-09-27 18:23:49

我需要从代码中找出项目是否在本地映射。我可以使用Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory.GetTeamProjectCollection()获得所有TFS项目,而不是使用workItemStore = new WorkItemStore(projects)获得foreach,并获得许多项目信息,但可以使用类似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;
    }

更新:答案

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 
    }

TFS中项目映射的编程检查方式

这更像是一种通用的方法,但我认为你会设法根据自己的需求定制它(不是编译的,只是指向方向):

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