GetLocalWorkspaceInfo始终为null

时间:2016-11-17 20:21:43

标签: c# tfs visual-studio-2015 tfs2013

我的问题与TFS API: GetLocalWorkspaceInfo always returns null非常相似,除了我使用的是2015年的Visual Studio,所以这些答案对我来说并不适用。我尝试了GetAllLocalWorkspaceInfo,它也返回null。

由于

2 个答案:

答案 0 :(得分:0)

我已经测试了您在VS 2015中引用的代码段并获得了成功的结果。确保你参考了VS 2015中的dll:c:\program files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\T‌​eam Explorer

在代码段中,您需要使用Console.WriteLine输出所需的工作区信息:

private static Workspace FindWorkspaceByPath(TfsTeamProjectCollection tfs, string workspacePath)
        {

            VersionControlServer versionControl = tfs.GetService<VersionControlServer>();

            WorkspaceInfo workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(workspacePath);

            if (workspaceInfo != null)
            {
                Console.WriteLine(workspaceInfo.Computer);
                Console.WriteLine(workspaceInfo.DisplayName);
                return versionControl.GetWorkspace(workspaceInfo);
            }

            //No Workspace found using method 1, try to query all workspaces the user has on this machine.
            Workspace[] workspaces = versionControl.QueryWorkspaces(null, Environment.UserName, Environment.MachineName);
            foreach (Workspace w in workspaces)
            {
                foreach (WorkingFolder f in w.Folders)
                {
                    if (f.LocalItem.Equals(workspacePath))
                    {
                        return w;
                    }
                }
            }

            throw new Exception(String.Format("TFS Workspace cannot be determined for {0}.", workspacePath));
        }

答案 1 :(得分:-2)

我发现此问题是因为包版本,所以如果您遇到此问题,请检查您的包的版本是否与您的Visual Studio版本相匹配