gpt4 book ai didi

c# - 如何以编程方式检查项目是否映射到 TFS 中?

转载 作者:太空狗 更新时间:2023-10-29 23:32:01 24 4
gpt4 key购买 nike

我需要从代码中找出项目是否映射到本地。我可以使用 Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory.GetTeamProjectCollection() 获取所有 TFS 项目,比我能做的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;
}

UPD:回答

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
}

最佳答案

这是一种更通用的方法,但我认为您将设法根据您的需要对其进行自定义(未编译,只是指明方向):

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

关于c# - 如何以编程方式检查项目是否映射到 TFS 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17741619/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com