Reading the Version number from a AssemblyInfo.cs file

how to update assembly version c#
c# get assembly version
wpf get assembly version
assembly information c#
c# get assembly description
c# assemblyfileversion
c# get assembly title
c# get assembly copyright

I'm trying to extract the version number from a AssemblyInfo.cs file! And I'm trying to use System.Reflection.Assembly.LoadFile(path); But while doing this I get a BadImageFormatException; "The module was expected to contain an assembly manifest. (Exception from HRESULT: 0x80131018)". So now I wounder, is that not a possible way to go about it? And should I use RegEx instead?

I have read many examples with GetExecutingAssembly() but I do want to get the version from an other project.

Clarification: I want to read the version info from the AssemblyInfo.cs file! And not from a compiled file. I'm trying to make a tool to update my version numbers before I make a new release.


You can get Assembly version without loading it as:

using System.Reflection;
using System.IO;

...

// Get assembly 
AssemblyName currentAssembly = AssemblyName.GetAssemblyName(path);
Version assemblyVersion = currentAssembly.Version;

Edit: If you want to read file then you can do it like this:

string path = @"d:\AssemblyInfo.cs";
            if (File.Exists(path))
            {
                // Open the file to read from.
                string[] readText = File.ReadAllLines(path);
                var versionInfoLines = readText.Where(t => t.Contains("[assembly: AssemblyVersion"));
                foreach (string item in versionInfoLines)
                {
                    string version = item.Substring(item.IndexOf('(') + 2, item.LastIndexOf(')') - item.IndexOf('(') - 3);          
                    //Console.WriteLine(Regex.Replace(version, @"\P{S}", string.Empty));
                    Console.WriteLine(version);
                }

            }

//Output

1.0.*
1.0.0.0

Hope this help...

Reading the Version number from a AssemblyInfo.cs file, I'm trying to extract the version number from a AssemblyInfo.cs file! And I'm trying to use System.Reflection.Assembly.LoadFile(path); But while doing this I get a� The filename AssemblyInfo.cs is used by convention as the source file where developers place metadata attributes that describe the entire assembly they are building. [AssemblyConfiguration] AssemblyConfiguration: The AssemblyConfiguration attribute must have the configuration that was used to build the assembly.


You can specify the target assembly path in AssemblyName.GetAssemblyName

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

Use AssemblyVersion and AssemblyFileVersion, 04/14/2020; 2 minutes to read Summary. AssemblyInfo.cs provides two attributes to set two different types of versions. This tip shows Use the assembly file version number to communicate the latest release of assembly. While both Jenkins and TeamCity include or have available plugins for updating AssemblyInfo.cs, they both suffer from the problem in that they can write a version into the file but they can't read from it first to derive a new value. However, if you simply want to set a full version from within either CI tool you can without having to bother


AssemblyInfo.cs file gets compiled to IL assembly.

If you load that assembly you can read the version with all the examples that you have already seen. Which is reading an embedded version information from a compiled assembly file, and it may be overwritten by compilation process to a value different from what is in AssemblyInfo.cs

However it sounds like what you want instead is to read a version number from AssemblyInfo.cs text file, without compiling it down.

If this is the case you really just have to use regex with a format appropriate for your project, or even come up with a convention that will keep it simple.

This could be as simple as

var versionMatch = Regex.Match(File.ReadAllText(filename), @"AssemblyVersion\s*\(\s*""([0-9\.\*]*?)""\s*\)");
if (versionMatch.Success)
{
    Console.WriteLine(versionMatch.Groups[1].Value);
}

You would have to consider convention around what goes there, since 1.0.* is a valid version string that translates to timestamp values of form 1.0.nnn.mmm at compile time, and nnn and mmm part closely guessable but not precisely guessable.

How to use Assembly Version and Assembly File Version, Assembly Version : This is the version number used by framework during build and at runtime to locate, link and load the assemblies. When you� Assembly Version [AssemblyVersion] Specifies the version of the assembly. File Version [AssemblyFileVersion] Specifies a version number that instructs the compiler to use a specific version for the Win32 file version resource. GUID [Guid] A unique GUID that identifies the assembly. When you create a project, Visual Studio generates a GUID for


It sounds like you're trying to load an assembly compiled for x86 in an x64 environment or vice-versa.

Ensure the assembly this code resides in is built for the same environment as the target and you can get it with the examples it sounds like you've read.

Updating AssemblyInfo.cs version information via batch file, Updating AssemblyInfo.cs version information via batch file The "simplest" way of reading text from a file in a batch script is to use a Unix utility and definitely do not want bindings to break from build number changes. AssemblyInfo.cs contains information about your assembly, like name, description, version, ecc. You can find more details about its content reading the comments that are included in it. You can find more details about its content reading the comments that are included in it.


You can proceed with Assembly.GetName().Version where your assembly could be the type of your class

public class Test
{
    public static void Main()
    {
        Console.WriteLine("Current assembly : " + typeof(Test).Assembly.GetName().Version);
    }
}

For the test application I have working on, shows me below details using above code:

Reading Values from AssemblyInfo.cs – Lance Larsen – Microsoft , 2: /// Gets the values from the AssemblyInfo.cs file for the current executing Version; 14: /// string versionFull = AssemblyInfo.VersionFull; 76: // Default to "3 " as the number of levels back in the stack trace to get the The XML above defines the location of the AssemblyInfo.cs file. The most interesting part of it is the Compile item. It instructs MSBuild that the AssemblyInfo.cs file should be passed to the C# compiler. The presence of this line will allow us to remove the AssemblyInfo.cs from the project, but more on this later.


AssemblyInfo.cs Examples, The filename AssemblyInfo.cs is used by convention as the source file where developers place metadata a portion of the version automatically every time you compile (often used for the "build" number) Reading Assembly Attributes. In C# the wildcard on the version number tells the compiler to automatically update the version number -- if there's no wildcard, it will just use the full number supplied. e.g. [assembly: AssemblyVersion("1.0.3.10")] Will always use that version number until you change it in the file.


Getting to Know Your Assembly by Name and Number -- Visual , This file includes human-friendly fields that help identify the assembly: Name, Description, Copyright, Version Number and so on. It is exactly the� Create a version file (or use assemblyinfo.cs). The version information would be in the format . This file will be checked into my VCS. Run a TeamCity build; TeamCity reads the version file from step 1, picks out each individual numbers and sets corresponding TeamCity build parameters (so I can use them later for things such as creating


Get App Version in .NET Core, We can define the version numbers for AssemblyVersion, FileVersion and Version in the project file (.csproj). With this in mind, lets see how can we read these values in runtime. 1. GetType().Assembly.GetName().Version.ToString(). But if you need to use this in a static method, you may have to create a� To use values from a project, specify the tokens described in the table below (AssemblyInfo refers to the file in Properties such as AssemblyInfo.cs or AssemblyInfo.vb). To use these tokens, run nuget pack with the project file rather than just the .nuspec .