Get TFS mapped folder of a local sub folder of the

2019-07-16 07:47发布

问题:

Let's say we have a solution in TFS Source Control which has already been mapped to a local folder SolutionFolder.

We are in a sub folder SubFolder of this SolutionFolder. How can we write C# code to get the mapped path of this SubFolder?

回答1:

Use the WorkStation.Current to grab the information for the folder in question:

Import the following namespaces:

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;

and then use you can get to the data you want through:

var workspace = Workstation.Current.GetLocalWorkspaceInfo(solutionFolder);
if (workspace != null)
{
    var teamProjectUri = workspace.ServerUri;

    // var server = TfsConfigurationServerFactory.GetConfigurationServer(teamProjectUri);
    var projectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(teamProjectUri);   
    var cssService = projectCollection.GetService<ICommonStructureService4>();
    var project = cssService.GetProjectFromName(solutionName);
}

From there you can easily grab the Workspace as well and from there the serverpath: workspace.GetWorkspace().GetServerItemForLocalItem()

To provide credentials, you can use one of the additional overloads that accepts a CredentialsProvider. The default provider is the UICredentialsProvider. Or you can also call server or projectCollection's EnsureAuthenticated.

See also:

  • https://jessehouwing.net/vsts-tfs-api-auto-detect-connection-details/