通过TFS API识别“当前”打开工作区?

时间:2023-01-24 13:24:06

Is there a way to programatically determine the current workspace of the open sln/proj in visual studio using the TFS API? I've seen how the VersionControlServer can retreive all of the known workspaces, but is there anything I can use to tie that to what the user currently has (or doesn't have) open?

有没有办法使用TFS API以编程方式确定visual studio中open sln / proj的当前工作空间?我已经看到VersionControlServer如何可以检索所有已知的工作空间,但是有什么我可以用来将它与用户当前拥有(或没有)打开的东西联系起来吗?

3 个解决方案

#1


1  

If you can determine the physical path of the solution or project file, then you can query that file in TFS and you should see which workspace has been mapped to that local file location.

如果您可以确定解决方案或项目文件的物理路径,则可以在TFS中查询该文件,并且应该看到哪个工作空间已映射到该本地文件位置。

#2


11  

There is another override to the GetWorkspace method of an instantiated VersionControlServer object. You can call GetWorkspace with the local path like Bernhard states, but you can also call it with the workspace name and workspace owner. Since the workspace name defaults to the local computer name, you can usually get away with using Environment.MachineName, but there is always going to be that developer who changes the workspace name.

实例化的VersionControlServer对象的GetWorkspace方法还有另一个覆盖。您可以使用Bernhard状态等本地路径调用GetWorkspace,但您也可以使用工作区名称和工作区所有者来调用它。由于工作空间名称默认为本地计算机名称,因此通常可以使用Environment.MachineName,但总会有更改工作空间名称的开发人员。

Example:

例:

TeamFoundationServerFactory _tfs = TeamFoundationServerFactory.GetServer(server);
            _tfs.EnsureAuthenticated();

VersionControlServer _vcs = (VersionControlServer)_tfs.GetService(typeof(VersionControlServer));
Workspace _ws = _vcs.GetWorkspace(Environment.MachineName, Environment.UserName);

#3


1  

The problem with the approach from Dave Teply is that it assumes you already have an instance of a VersionControlServer or at least the TeamFoundationServerUri.

Dave Teply方法的问题在于它假设您已经拥有VersionControlServer或至少TeamFoundationServerUri的实例。

There is a more powerful way though, using the Workstation class. Ricci Gian Maria has written a quite extensive blog post about this topic. The below snippet is the essentials of that post:

但是,使用Workstation类有一种更强大的方法。 Ricci Gian Maria撰写了一篇关于这个主题的博文。以下片段是该帖子的基本要点:

Use the Workstation class to get the WorkspaceInfo for the path you're looking for, this will search the workpaces for all TFS servers registered on that workstation to see if there's a match:

使用Workstation类获取您正在寻找的路径的WorkspaceInfo,这将在工作空间中搜索在该工作站上注册的所有TFS服务器,以查看是否存在匹配:

Workstation workstation = Workstation.Current;
WorkspaceInfo info = workstation.GetLocalWorkspaceInfo(path);

Now that you have the WorkspaceInfo, you can sue it to connect to TFS, the workspace info contains the ProjectCollectionUri for that specific team project collection. And from that the Workspace instance:

现在您已拥有WorkspaceInfo,您可以起诉它以连接到TFS,工作区信息包含该特定团队项目集合的ProjectCollectionUri。从那个Workspace实例:

TfsTeamProjectCollection collection = new TfsTeamProjectCollection(info.ServerUri);
Workspace workspace = info.GetWorkspace(collection);

Use the collection or the workspace to then get access to the VersionControlServer:

使用集合或工作空间然后可以访问VersionControlServer:

VersionControlServer versionControlServer = collection.GetService<VersionControlServer>();

#1


1  

If you can determine the physical path of the solution or project file, then you can query that file in TFS and you should see which workspace has been mapped to that local file location.

如果您可以确定解决方案或项目文件的物理路径,则可以在TFS中查询该文件,并且应该看到哪个工作空间已映射到该本地文件位置。

#2


11  

There is another override to the GetWorkspace method of an instantiated VersionControlServer object. You can call GetWorkspace with the local path like Bernhard states, but you can also call it with the workspace name and workspace owner. Since the workspace name defaults to the local computer name, you can usually get away with using Environment.MachineName, but there is always going to be that developer who changes the workspace name.

实例化的VersionControlServer对象的GetWorkspace方法还有另一个覆盖。您可以使用Bernhard状态等本地路径调用GetWorkspace,但您也可以使用工作区名称和工作区所有者来调用它。由于工作空间名称默认为本地计算机名称,因此通常可以使用Environment.MachineName,但总会有更改工作空间名称的开发人员。

Example:

例:

TeamFoundationServerFactory _tfs = TeamFoundationServerFactory.GetServer(server);
            _tfs.EnsureAuthenticated();

VersionControlServer _vcs = (VersionControlServer)_tfs.GetService(typeof(VersionControlServer));
Workspace _ws = _vcs.GetWorkspace(Environment.MachineName, Environment.UserName);

#3


1  

The problem with the approach from Dave Teply is that it assumes you already have an instance of a VersionControlServer or at least the TeamFoundationServerUri.

Dave Teply方法的问题在于它假设您已经拥有VersionControlServer或至少TeamFoundationServerUri的实例。

There is a more powerful way though, using the Workstation class. Ricci Gian Maria has written a quite extensive blog post about this topic. The below snippet is the essentials of that post:

但是,使用Workstation类有一种更强大的方法。 Ricci Gian Maria撰写了一篇关于这个主题的博文。以下片段是该帖子的基本要点:

Use the Workstation class to get the WorkspaceInfo for the path you're looking for, this will search the workpaces for all TFS servers registered on that workstation to see if there's a match:

使用Workstation类获取您正在寻找的路径的WorkspaceInfo,这将在工作空间中搜索在该工作站上注册的所有TFS服务器,以查看是否存在匹配:

Workstation workstation = Workstation.Current;
WorkspaceInfo info = workstation.GetLocalWorkspaceInfo(path);

Now that you have the WorkspaceInfo, you can sue it to connect to TFS, the workspace info contains the ProjectCollectionUri for that specific team project collection. And from that the Workspace instance:

现在您已拥有WorkspaceInfo,您可以起诉它以连接到TFS,工作区信息包含该特定团队项目集合的ProjectCollectionUri。从那个Workspace实例:

TfsTeamProjectCollection collection = new TfsTeamProjectCollection(info.ServerUri);
Workspace workspace = info.GetWorkspace(collection);

Use the collection or the workspace to then get access to the VersionControlServer:

使用集合或工作空间然后可以访问VersionControlServer:

VersionControlServer versionControlServer = collection.GetService<VersionControlServer>();