C # code to check for workspace in TFS

I am trying to create an automation tool to get the latest code from TFS. I need to check if any workspace with the same name exists in the system. If an instance of the workspace exists. Otherwise, create a workspace and mappings.

I found that I Microsoft.TeamFoundation.VersionControl.ClientVersionControlServerhave a method Workspace GetWorkspace(string workspaceName, string workspaceOwner);for getting an existing workspace. But this will throw an exception if the workspace does not exist in the system.

So give me a code that checks for the existence of a workspace and mappings.

I currently have the following code that I know, its the wrong 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;

}
+4
1

, QueryWorkspaces

 workspace = versionControl.QueryWorkspaces(
                     workspaceName, 
                     versionControl.AuthorizedUser, 
                     Environment.MachineName).SingleOrDefault();

, . , null . , QueryWorkspaces (, ), , , .

  if (workspace !=null)
  {
       foreach(var folder in workspace.Folders)
       {
             if (!folder.IsCloaked && folder.LocalItem != "some expected path")
             {
                  // mapping invalid, throw/log?
             }
       }
  }
+5

Source: https://habr.com/ru/post/1526707/


All Articles