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