In .NET, an application has a number of different versions, and so we have a number of alternative ways to access them. The version number that we discussed earlier and is displayed in the Publish Version section of the Publish tab of the project properties can be found using the ApplicationDeployment class from the System.Deployment DLL:
using System.Deployment.Application; ... private string GetPublishedVersion() { if (ApplicationDeployment.IsNetworkDeployed) { return
ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString(); } return "Not network deployed"; }
Note that we need to verify that the application has actually been deployed before we can access the CurrentVersion property of the ApplicationDeployment class, otherwise an InvalidDeploymentException will be thrown. This means that we cannot attain the published version while debugging our WPF applications, and so we should return some other value instead in these instances.
In order to view the remaining application versions, we first need to access the assembly that we want to know the version of. The code that we use to access the assembly will depend on where in the code we currently are. For example, we typically want to display the version of the startup assembly, but we may want to access it from a View Model in the ViewModels project instead.
We have a number of ways of accessing assemblies, depending on where they are in relation to the calling code. If we want to access the startup assembly from the startup project, then we can use the Assembly.GetExecutingAssembly method after adding using statements for the following namespaces:
using System.Diagnostics; using System.Reflection;
To access the same assembly from a different project, we can use the Assembly.GetEntryAssembly method. Alternatively, we can access the startup project's assembly from a different project (if that project was called from the startup assembly) using the Assembly.GetCallingAssembly method. For the remaining examples here, we'll use the GetEntryAssembly method.
In addition to the published version, we may also need to access the application's assembly or file versions. The assembly version that we can set in the Assembly Information dialog window, which is accessible from the Application tab of the project properties window, can be accessed from the assembly using the following code:
string assemblyVersion = Assembly.GetEntryAssembly().GetName().Version.ToString();
The assembly version is used by the .NET Framework to load and link references to other assemblies at build and runtime. This is the version that is embedded when adding references to our projects in Visual Studio and if an incorrect version is found during a build, then an error will be raised.
Note that we can also set this value using the assembly level AssemblyVersionAttribute class in the AssemblyInfo.cs file of the project, which can be found in the Properties node of the project in the Solution Explorer.
Instead of converting the returned Version object to a string directly, we may prefer to access the individual components that make up the version number. They comprise the Major, Minor, Build, and Revision component values.
We could then chose to just output the Major and Minor components, along with the product name. Here's an example:
Version assemblyVersion = Assembly.GetEntryAssembly().GetName().Version; string productName = FileVersionInfo.GetVersionInfo( Assembly.GetEntryAssembly().Location).ProductName; string output = $"{productName}: Version {version.Major}.{version.Minor}";
If we need the file version, which is used for non-ClickOnce deployments, we can pass the location of the assembly to the GetVersionInfo method of the FileVersionInfo class, as shown in the preceding code in the product name example, but access the FileVersion property instead:
string fileVersion = FileVersionInfo.GetVersionInfo( Assembly.GetEntryAssembly().Location).FileVersion;
Note that we can also set this value in the Assembly Information dialog window, or by using the assembly level AssemblyFileVersionAttribute class in the AssemblyInfo.cs file of the project. This version can be seen in the Details tab of the file properties dialog window in Windows Explorer:

The product version that the assembly is distributed with can be accessed in a similar way:
string productVersion = FileVersionInfo.GetVersionInfo( Assembly.GetEntryAssembly().Location).ProductVersion;
Note that this version can also be seen in the Details tab of the file properties dialog window in Windows Explorer, along with the product name that we accessed earlier. Also note that in a WPF application, this value typically comes from the assembly file version.