TFS API: GetLocalWorkspaceInfo always returns null

2019-02-02 09:30发布

On one of my machines, I get a return value of null from any GetLocalWorkspaceInfo call. I have isolated to problem to where it even fails for this simple program:

namespace WorkstationTest
{
    using Microsoft.TeamFoundation.VersionControl.Client;

    class Program
    {
        static void Main()
        {
            string workspaceLocalPath = @"C:\Dev";
            var info = Workstation.Current
                          .GetLocalWorkspaceInfo(workspaceLocalPath);

            // info is always null here
        }
    }
}

What I have already checked:

  • The exact same code works on my other machine the way it should.

  • I have verified that I have a workspace at C:\Dev

    Workspace Screenshot

  • I have created a new workspace and in a different directory and changed the workspaceLocalPath variable in the code to match.

  • I have consulted the documentation which states that the return value will be null if the path is not in a workspace. From the above image, the path should be in a workspace.

Yet, everything seems to suggest this should work. Is there anything I could be missing?

标签: c# tfs-sdk
7条回答
▲ chillily
2楼-- · 2019-02-02 10:05

This is how to find workspace when you have server path:

  Workspace[] workspaces = _versionControl.QueryWorkspaces(null, Environment.UserName, Environment.MachineName);
  return workspaces.FirstOrDefault(w => !string.IsNullOrEmpty(w.TryGetLocalItemForServerItem(ConstDefaultFlowsTfsPath)));

Where ConstDefaultFlowsTfsPath is server path with "$" for instance : "$/MyCompany/Services/DiagnosticsFlows"

You could also replace the last line to:

return workspaces.FirstOrDefault(w => !string.IsNullOrEmpty(w.GetServerItemForLocalItem(myLocalPath)));

and that should work for you too.

查看更多
登录 后发表回答