且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用于检查工作区是否存在于 TFS 上的 C# 代码

更新时间:2023-11-25 15:23:46

如果您不想依赖捕获异常,可以调用 查询工作区

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?
             }
       }
  }