简体   繁体   中英

Get TFS version programmatically

How can I get the TFS version programmatically?

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

TFS管理控制台

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...

我正在使用Microsoft.TeamFoundation.Server.dll版本号,然后使用此链接中的表。

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

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM