0

How can I get the TFS version programmatically?

I am trying to get the version that shows up in the TFS Administration console.

TFS Admin console

I tried the following code, but it returns the server version as "Server Version: Dev14.M89-Part7", that doesn't seem correct.

var server = new TfsTeamProjectCollection(new Uri("http://tfs2015:8080/tfs"));
server.EnsureAuthenticated();
var serverVersion = server.ServerDataProvider.ServerVersion;
Console.WriteLine("Server Version: {0}", serverVersion);

I guess I am looking at the wrong property...

Tarun Arora
  • 4,692
  • 4
  • 30
  • 40

3 Answers3

1

I'm using the Microsoft.TeamFoundation.Server.dll version number then the table from this link.

ds19
  • 3,176
  • 15
  • 33
  • If the link dies, then your answer loses its worth. Please add the relevant parts from the link to your answer. – DeanOC Nov 27 '15 at 02:22
  • If you had an application that was being used between two versions of TFS, this dll version number approach won't work. – Tarun Arora Nov 27 '15 at 09:42
1

Unfortunately there is not some uniform method that you can call that will simply tell you “You are communicating with version X of TFS”. In order to determine what version of the server you are talking to we are going to use the principals about querying for services along with some knowledge about what services were available in each release.

Check this blog:http://blogs.msdn.com/b/taylaf/archive/2010/01/05/determining-the-tfs-server-version-using-client-apis.aspx

Cece Dong - MSFT
  • 29,631
  • 1
  • 24
  • 39
1

Another approach can be to pick the version number from a DLL, but requires to reach the server via PSExec, CIFS/SMB or Powershell Remoting.

The C# code should be something like

using (var tfsBaseKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\TeamFoundationServer"))
{
    var versionKeys = tfsBaseKey.GetSubKeyNames();
    double dummy;
    double maxVersion = versionKeys.Max(x => double.TryParse(x, out dummy) ? dummy : 0.0);
    var latestVersionKey = maxVersion.ToString("#.0");
    using (var tfsKey = tfsBaseKey.OpenSubKey(latestVersionKey))
    {
        string tfsInstallPath = tfsKey.GetValue("InstallPath").ToString();
        string refAssemblyPath = Path.Combine(tfsInstallPath, @"Application Tier\Web Services\bin\Microsoft.TeamFoundation.Server.Core.dll");
        var refAssembly = Assembly.ReflectionOnlyLoadFrom(refAssemblyPath);
        var fileVer = refAssembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false).FirstOrDefault() as AssemblyFileVersionAttribute;

        return fileVersion.Version;
    }
}
Giulio Vian
  • 8,248
  • 2
  • 33
  • 41