问题
I need to get Team Foundation Server list programmatically with C#.
This is needed for the TfsTeamProjectCollection
object.
回答1:
I had a scan through the documentation and couldn't find anything useful. I believe @samy is correct and there is no discovery mechanism.
However if you are running this on a client machine that already has established connections to TFS then there is a history of servers stored in the registry:
Visual Studio 2008 location:
HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\TeamFoundation\Servers
Visual Studio 2010:
HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\10.0\TeamFoundation\Instances\
Visual Studio 2012
HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\11.0\TeamFoundation\Instances\
Visual Studio 2013
HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\12.0\TeamFoundation\Instances\
Visual Studio 2015
HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\14.0\TeamFoundation\Instances\
回答2:
I think the responsibility of keeping track of the servers is your responsibility. There's no discovery options for the servers as far as i know. So you need the uris
回答3:
For Visual Studio 2012, the location in the registry key is:
HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\11.0\TeamFoundation\Instances\tfs
回答4:
To list all workspaces that "tf workspaces" would return (which includes URI and mapped paths):
var allWorkspaceInfos = Workstation.Current.GetAllLocalWorkspaceInfo();
To get the TFS workspace containing a particular directory:
var workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(@"C:\Users\joey\tfs");
I found this by decompiling TF.exe. But there is documentation for the Workstation class.
These methods return WorkspaceInfo objects. To perform source control operations, you need to connect to the server and get a Workspace object:
var collection = new TfsTeamProjectCollection(workspaceInfo.ServerUri);
collection.Authenticate();
var server = collection.GetService<VersionControlServer>();
var workspace = server.GetWorkspace(scriptRoot);
来源:https://stackoverflow.com/questions/7951419/how-do-i-get-a-list-of-team-foundation-server-servers-available-on-my-pc