Reading the Version number from a AssemblyInfo.cs file

11

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.

c#
c#-4.0
asked on Stack Overflow May 8, 2012 by Markus • edited May 8, 2012 by Markus

6 Answers

9

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...

answered on Stack Overflow May 8, 2012 by Niranjan Singh • edited Feb 25, 2019 by Niranjan Singh
6

You can specify the target assembly path in AssemblyName.GetAssemblyName

AssemblyName.GetAssemblyName("ProjectB.exe").Version
answered on Stack Overflow May 8, 2012 by ABH • edited May 8, 2012 by ABH
1

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.

answered on Stack Overflow Feb 16, 2019 by aiodintsov
0

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.

answered on Stack Overflow May 8, 2012 by Steve Danner
0

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:

enter image description here

answered on Stack Overflow Feb 15, 2019 by VikrantMore • edited Feb 15, 2019 by VikrantMore
0

Here is article which explains the code to read values from AssemblyInfo.cs class.

http://lancelarsen.com/reading-values-from-assemblyinfo-cs/

Hope it helps.

answered on Stack Overflow Feb 21, 2019 by Mohit Verma

User contributions licensed under CC BY-SA 3.0