Jacob JA Shanks
Jacob JA Shanks

Reputation: 369

How to get the version number of Visual Studio that built an application

I'm trying to put a build stamp on the bottom of my application with details like the build date, framework version and also the Visual Studio version (i.e., the version of Visual Studio used to build the app).

Previously this was hard coded but now that we are migrating, making this programmatic is a good opportunity. I have managed to do the former two attributes like so:

Dim targetFrameworkAttribute As TargetFrameworkAttribute =
Assembly.GetExecutingAssembly().GetCustomAttributes(GetType(TargetFrameworkAttribute), False).SingleOrDefault()

lblBuildDate.Text = $"Build date: {mdBuildDate} - [ VS2017 ] [ {targetFrameworkAttribute.FrameworkDisplayName.Replace(" Framework", "")} ]"

(mdBuildDate is collected from the database as a string)

But I am struggling to find a way of collecting the Visual Studio version from the assembly or elsewhere.

Does anyone know if this is possible?

Upvotes: 2

Views: 2830

Answers (3)

Srinivasan Jayakumar
Srinivasan Jayakumar

Reputation: 50

I hope this will help

internal static string GetVisualStudioInstalledPath()
{
    var visualStudioInstalledPath = string.Empty;
    var visualStudioRegistryPath = Registry.LocalMachine.OpenSubKey(
                                   @"SOFTWARE\WOW6432Node\Microsoft\VisualStudio\14.0");
    if (visualStudioRegistryPath != null)
    {
        visualStudioInstalledPath = visualStudioRegistryPath.GetValue("InstallDir", string.Empty) 
                                                                      as string;
    }

    if(string.IsNullOrEmpty(visualStudioInstalledPath) || 
                            !Directory.Exists(visualStudioInstalledPath))
    {
        visualStudioRegistryPath = Registry.LocalMachine.OpenSubKey(
                                   @"SOFTWARE\Microsoft\VisualStudio\14.0");
        if (visualStudioRegistryPath != null)
        {
            visualStudioInstalledPath = visualStudioRegistryPath.GetValue("InstallDir", 
                                                                          string.Empty) as string;
        }
    }

    if (string.IsNullOrEmpty(visualStudioInstalledPath) || 
                             !Directory.Exists(visualStudioInstalledPath))
    {
        visualStudioRegistryPath = Registry.LocalMachine.OpenSubKey(
                                   @"SOFTWARE\WOW6432Node\Microsoft\VisualStudio\12.0");
        if (visualStudioRegistryPath != null)
        {
            visualStudioInstalledPath = visualStudioRegistryPath.GetValue("InstallDir", 
                                                                          string.Empty) as string;
        }
    }

    if (string.IsNullOrEmpty(visualStudioInstalledPath) || 
                             !Directory.Exists(visualStudioInstalledPath))
    {
        visualStudioRegistryPath = Registry.LocalMachine.OpenSubKey(
                                   @"SOFTWARE\Microsoft\VisualStudio\12.0");
        if (visualStudioRegistryPath != null)
        {
            visualStudioInstalledPath = visualStudioRegistryPath.GetValue("InstallDir", 
                                                                          string.Empty) as string;
        }
    }

    return visualStudioInstalledPath;
}

Upvotes: -2

TnTinMn
TnTinMn

Reputation: 11801

The targeted framework version and the Visual Studio version are available during the build operation as MSBuild properties as either defined or reserved properties. These properties can be made available for use in a T4 text template for code generation.

The following procedure is based on VS2017 Community Edition.

  1. Add a Text Template to your project. Project Menu-> Add New Item ->General -> Text Template. Name the file ProjectInfo.tt.
  2. From the Solution Explorer window, select ProjectInfo.tt and right-click on its and select "Properties". Change the "Build Action" from Content to None.
  3. Replace the contents of ProjectInfo.tt with the following and save the file. You will receive an error of "An expression block evaluated as Null", ignore it.
<#@ template debug="false" hostspecific="true" language="VB" #>
<#@ parameter type="System.String" name="VisualStudioVersion" #>
<#@ parameter type="System.String" name="TargetFrameworkVersion" #>
<#@ assembly name="System.Core" #>
<#@ output extension=".vb" #>
Module ProjectInfo
    Public ReadOnly VisualStudioVersion As String = "<#= VisualStudioVersion #>"
    Public ReadOnly TargetFrameworkVersion As String = "<#= TargetFrameworkVersion #>"
    Public ReadOnly BuildDate As New DateTime(<#= DateTime.Now().Ticks #>L)
End Module
  1. Save the project and select the it in the Solution Explorer window and right-click on it. Select "Unload Project". Right-click it again and select "Edit projname.vbproj".
  2. Scroll down to the end of the file add the following before the "" tag.
  <!-- Define the parameters available T4 Text templates  -->
  <ItemGroup>
    <T4ParameterValues Include="VisualStudioVersion">
      <Value>$(VisualStudioVersion)</Value>
      <Visible>false</Visible>
    </T4ParameterValues>
    <T4ParameterValues Include="TargetFrameworkVersion">
      <Value>$(TargetFrameworkVersion)</Value>
      <Visible>false</Visible>
    </T4ParameterValues>
  </ItemGroup>

  <!-- the following will cause the T4 template to be processed before the build process begins  -->
  <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TextTemplating\Microsoft.TextTemplating.targets" />
  <PropertyGroup>
    <TransformOnBuild>true</TransformOnBuild>
    <TransformOutOfDateOnly>false</TransformOutOfDateOnly>
  </PropertyGroup>
  1. Save and close the "vbproj" file. Right-click on the project in the Solution Explorer window and select "Reload project".
  2. Right-click on the project in the Solution Explorer window and select "Rebuild project".
  3. In the Solution Explorer window, enable "Show all files" and expand the "ProjectInfo.tt" node and open the "ProjectInfo.vb" file. you should see the following code (the assigned values may differ based on you project configuration).
Module ProjectInfo
    Public ReadOnly VisualStudioVersion As String = "15.0"
    Public ReadOnly TargetFrameworkVersion As String = "v4.72"
    Public ReadOnly BuildDate As New DateTime(636968364980609475L)
End Module

Once you have successfully built your project, the values defined in "ProjectInfo.vb" will be accessible by other code. The file will be regenerated on each build.

Reference Articles:

  1. Code Generation and T4 Text Templates

  2. Pass build context data into the templates


Edit: As an alternative to editing the projname.vbproj file, you could also place the statements presented under Step 5 in a text file named Directory.Build.targets that would be place in the project folder. The contents need to be enclosed in a <Project> tag.

<Project>
statements from Step 5
</Project> 

Upvotes: 3

djv
djv

Reputation: 15782

You can get it from the standard output of vswhere.exe

Dim proc = New Process() With
{
    .StartInfo = New ProcessStartInfo() With
    {
        .FileName = "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe",
        .UseShellExecute = False,
        .RedirectStandardOutput = True,
        .CreateNoWindow = True
    }
}
Dim lines = New Dictionary(Of String, String)()
proc.Start()
While Not proc.StandardOutput.EndOfStream
    Dim line = proc.StandardOutput.ReadLine()
    If line.Contains(": ") Then
        Dim split = line.Split(": ", StringSplitOptions.None)
        lines.Add(split(0), split(1))
    End If
End While
Dim installationVersion = lines("installationVersion")

by combining this answer and this MSDN post by Daniel Meixner.

Upvotes: 0

Related Questions