Reputation: 369
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
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
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.
<#@ 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
<!-- 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>
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:
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
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