问题描述
我正在尝试创建一个自动化工具来从 TFS 获取最新代码.我需要检查系统上是否存在同名的工作区.如果存在,获取工作区实例.否则创建工作区和映射.
I am trying to create a automation tool to get latest code from TFS. I need to check whether any workspace with same name exist on system. If exist get the workspace instance. Else create the workspace and mappings.
我发现 Microsoft.TeamFoundation.VersionControl.ClientVersionControlServer
有方法 Workspace GetWorkspace(string workspaceName, string workspaceOwner);
来获取现有的工作区.但是如果系统上不存在工作区,这将引发异常.
I found that Microsoft.TeamFoundation.VersionControl.ClientVersionControlServer
has method Workspace GetWorkspace(string workspaceName, string workspaceOwner);
to get existing workspace. But this will throw exception if workspace not exist on the system.
所以请给我一个检查工作区和映射是否存在的代码.
So please give me a code that checks the existence of workspace and mappings.
目前我有以下代码,我知道它的方法不正确
At present i have the following code which i know its not correct way
try
{
//**Expected** an exception as sometimes the workspace may not exist or Deleted manually.
workspace = versionControl.GetWorkspace(workspaceName, versionControl.AuthorizedUser);
versionControl.DeleteWorkspace(workspaceName, versionControl.AuthorizedUser);
workspace = null;
}
catch (Exception e)
{
DialogResult res = MessageBox.Show("There are no workspace mapped. I am creating a new workspace mapped to your local folder named DevFolder.", "Error", MessageBoxButtons.YesNo);
if (res == DialogResult.No)
{
return;
}
}
if (workspace == null)
{
var teamProjects = new List<TeamProject>(versionControl.GetAllTeamProjects(false));
// if there are no team projects in this collection, skip it
if (teamProjects.Count < 1)
{
MessageBox.Show("Please select a team project.");
return;
}
// Create a temporary workspace2.
workspace = versionControl.CreateWorkspace(workspaceName, versionControl.AuthorizedUser);
// For this workspace, map a server folder to a local folder
ReCreateWorkSpaceMappings(workspace);
createdWorkspace = true;
}
推荐答案
如果您不想依赖捕获异常,可以调用 查询工作区
If you don't want to rely on catching an exception you can call QueryWorkspaces
workspace = versionControl.QueryWorkspaces(
workspaceName,
versionControl.AuthorizedUser,
Environment.MachineName).SingleOrDefault();
此代码将在此代码运行的计算机上查询用户的工作区.如果集合为空,它将在工作区中返回 null 或将返回列表中的单个项目.在 QueryWorkspaces 返回更多项目(似乎不可能)的情况下,它仍然会抛出,但对我来说似乎没问题.
This code will query for the workspace for a user on the computer this code runs. If the collection is empty it will return null in workspace or it will return the single item in the list. In the case QueryWorkspaces returns more items (seems not possible) it will still throw but that seems OK to me.
现在您可以检查映射
if (workspace !=null)
{
foreach(var folder in workspace.Folders)
{
if (!folder.IsCloaked && folder.LocalItem != "some expected path")
{
// mapping invalid, throw/log?
}
}
}
这篇关于用于检查工作区是否存在于 TFS 上的 C# 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!