5

我需要找出是本地映射的项目还是不是从代码中映射的。我可以使用获取所有 TFS 项目Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory.GetTeamProjectCollection(),而不是获取大量项目信息,但可以使用或之类的任何东西。foreachworkItemStore = new WorkItemStore(projects)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 
    }
4

1 回答 1

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);
     }
  }
} 
于 2013-07-19T13:56:42.153 回答