How to Get the Assembly File Version

How can I get the assembly file version

See my comment above asking for clarification on what you really want. Hopefully this is it:

System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
System.Diagnostics.FileVersionInfo fvi = System.Diagnostics.FileVersionInfo.GetVersionInfo(assembly.Location);
string version = fvi.FileVersion;

Getting Assembly Version from AssemblyInfo.cs

As the documentation states, Assembly.GetExecutingAssembly() gets the assembly that the calling code was compiled inside of.

If you want to be more explicit (and faster), you can write typeof(SomeType).Assembly, where SomeType is any type in the project you're looking for.

How to get Assembly Version (not File Version) for another EXE?

From this blog article How to get assembly version without loading it:

AssemblyName.GetAssemblyName("filename.exe").Version

This avoids having to load the assembly in its entirity.

How to check the version of an assembly (dll)?

You can use Reflector, ILDASM or ILSpy to get the assembly version.

You usually can find ILDASM in C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\ildasm.exe (where v8.1A is the version of the Windows SDK installed).

ILDASM:

ildasm

Reflector:

reflector

How do I get AssemblyVersion stamped as FileVersion on the binary

According to Microsoft.NET.GenerateAssemblyInfo.targets project if you don't specify FileVersion property and set GenerateAssemblyFileVersionAttribute to false then FileVersion property value will be equal to AssemblyVersion property.

Try to modify you csproj file like this:

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>

<AssemblyVersion>1.0.*</AssemblyVersion>

<!-- Comment or delete this line. -->
<!-- <FileVersion>1.0.*</FileVersion> -->

<Deterministic>false</Deterministic>
<PackageId>Demo</PackageId>
<Company>My Company</Company>
<Copyright>Copyright © Xyzzy 2020</Copyright>
<Description>Description</Description>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>

<!-- Set this attribute to false. -->
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>

<GenerateAssemblyTitleAttribute>true</GenerateAssemblyTitleAttribute>
<GenerateAssemblyConfigurationAttribute>true</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>true</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>true</GenerateAssemblyProductAttribute>
<GenerateAssemblyCopyrightAttribute>true</GenerateAssemblyCopyrightAttribute>
<GenerateAssemblyVersionAttribute>true</GenerateAssemblyVersionAttribute>
<GenerateAssemblyInformationalVersionAttribute>true</GenerateAssemblyInformationalVersionAttribute>
</PropertyGroup>
</Project>

This approach worked for me. After making above changes to csproj file I was able to see FileVersion in the file properties window.

MSBuild: How to read Assembly Version and FIle Version from AssmeblyInfo.cs?


Using MSBuild script how can we get AssmeblyVersion and FileVersion from AssemblyInfo.cs?

To get the AssmeblyVersion via MSBuild, you can use GetAssemblyIdentity Task.

To accomplish this, unload your project. Then at the very end of the project, just before the end-tag </Project>, place below scripts:

  <Target Name="GetAssmeblyVersion" AfterTargets="Build">
<GetAssemblyIdentity
AssemblyFiles="$(TargetPath)">
<Output
TaskParameter="Assemblies"
ItemName="MyAssemblyIdentities"/>
</GetAssemblyIdentity>

<Message Text="Assmebly Version: %(MyAssemblyIdentities.Version)"/>
</Target>

To get the FileVersion, you could add a custom MSBuild Inline Tasks to get this, edit your project file .csproj, add following code:

  <UsingTask
TaskName="GetFileVersion"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">

<ParameterGroup>
<AssemblyPath ParameterType="System.String" Required="true" />
<Version ParameterType="System.String" Output="true" />
</ParameterGroup>
<Task>
<Using Namespace="System.Diagnostics" />
<Code Type="Fragment" Language="cs">
<![CDATA[
Log.LogMessage("Getting version details of assembly at: " + this.AssemblyPath, MessageImportance.High);

this.Version = FileVersionInfo.GetVersionInfo(this.AssemblyPath).FileVersion;
]]>
</Code>
</Task>
</UsingTask>

<Target Name="GetFileVersion" AfterTargets="Build">

<GetFileVersion AssemblyPath="$(TargetPath)">
<Output TaskParameter="Version" PropertyName="MyAssemblyFileVersion" />
</GetFileVersion>
<Message Text="File version is $(MyAssemblyFileVersion)" />
</Target>

After with those two targets, you will get the AssmeblyVersion and FileVersion from AssemblyInfo.cs:

Sample Image

Note:If you want to get the version of a specific dll, you can change the AssemblyFiles="$(TargetPath)" to the:

<PropertyGroup>
<MyAssemblies>somedll\the.dll</MyAssemblies>
</PropertyGroup>

AssemblyFiles="@(MyAssemblies)"

Hope this helps.

How can I get the executing assembly version?

Two options... regardless of application type you can always invoke:

Assembly.GetExecutingAssembly().GetName().Version

If a Windows Forms application, you can always access via application if looking specifically for product version.

Application.ProductVersion

Using GetExecutingAssembly for an assembly reference is not always an option. As such, I personally find it useful to create a static helper class in projects where I may need to reference the underlying assembly or assembly version:

// A sample assembly reference class that would exist in the `Core` project.
public static class CoreAssembly
{
public static readonly Assembly Reference = typeof(CoreAssembly).Assembly;
public static readonly Version Version = Reference.GetName().Version;
}

Then I can cleanly reference CoreAssembly.Version in my code as required.

How to automate Assembly File Version in C#

In Properties/AssemblyInfo.cs, just comment out the last line

[assembly: AssemblyFileVersion("1.0.0.0")]

so the end of the file reads

// You can specify all the values or you can default the Build and Revision Numbers 
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.*")]
//[assembly: AssemblyFileVersion("1.0.0.0")]

That way the Assembly version number will automatically be assigned as File version as well.

Get assembly file version from an archive without unpacking

Sorry but you can't without having a phisical file.

The only way to read the FileVersion is to use FileVersionInfo.GetVersionInfo which accept only a path.

And if you use reflector to see how it read the fileversion then you will see some unsafe native internal methods you cannot use...

private static string GetFileVersionString(IntPtr memPtr, string name)
{
int num;
string str = "";
IntPtr zero = IntPtr.Zero;
if (UnsafeNativeMethods.VerQueryValue(new HandleRef(null, memPtr), name, ref zero, out num) && (zero != IntPtr.Zero))
{
str = Marshal.PtrToStringAuto(zero);
}
return str;
}

Maybe you could get it with some DllImport. But this is not in my knowledge.

If you settle for AssemblyVersion you can use DotNetZip library:

Assembly assembly;
using (var data = new MemoryStream())
{
using (ZipFile zip = ZipFile.Read(LocalCatalogZip))
{
zip["assembly.dll"].Extract(data);
}
data.Seek(0, SeekOrigin.Begin);

assembly = Assembly.ReflectionOnlyLoad(data.ToArray());
}
var version = assembly.GetName().Version;

----------------UPDATE-----------------

Last thought: maybe you have permission to write a file in the temp folder: Path.GetTempPath

How do I get the version of an assembly without loading it?

I found the following in this article.

using System.Reflection;
using System.IO;

...

// Get current and updated assemblies
AssemblyName currentAssemblyName = AssemblyName.GetAssemblyName(currentAssemblyPath);
AssemblyName updatedAssemblyName = AssemblyName.GetAssemblyName(updatedAssemblyPath);

// Compare both versions
if (updatedAssemblyName.Version.CompareTo(currentAssemblyName.Version) <= 0)
{
// There's nothing to update
return;
}

// Update older version
File.Copy(updatedAssemblyPath, currentAssemblyPath, true);


Related Topics



Leave a reply



Submit