12

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.

Markus
  • 3,297
  • 4
  • 31
  • 52
  • Are you loading an .net Assembly and does assembly exist on the given path? – Deepesh May 08 '12 at 12:05
  • 3
    Hehe, compile it first, then you can use Assembly.Load(). If you really want to dig it out of AssemblyInfo.cs then write a text parser. – Hans Passant May 08 '12 at 12:07

5 Answers5

10

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

Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
6

You can specify the target assembly path in AssemblyName.GetAssemblyName

AssemblyName.GetAssemblyName("ProjectB.exe").Version
ABH
  • 3,391
  • 23
  • 26
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.

aiodintsov
  • 2,545
  • 15
  • 17
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.

Steve Danner
  • 21,818
  • 7
  • 41
  • 51
  • No I wan't the version info from a AssemblyInfo.cs file, Not from any compiled thing at all. I just wanted to know if I could use that method to read the file as well or if it was just for the compiled files. – Markus May 08 '12 at 12:11
  • 1
    Then Hans Passant is dead on with his comment, you're going to need to parse the raw text. – Steve Danner May 08 '12 at 12:21
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

VikrantMore
  • 903
  • 7
  • 25