Reading version number from AssemblyInfo.cs file

I am trying to extract the version number from AssemblyInfo.cs! And I'm trying to use. System.Reflection.Assembly.LoadFile(path);But in doing so, I get a BadImageFormatException exception; "The module is expected to contain an assembly manifest (exception to HRESULT: 0x80131018)." So, now I realized that this is not a possible way to do this? And should I use RegEx instead?

I have read many examples from GetExecutingAssembly(), but want to get a version from another project.

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

+3
source share
3 answers

You can get the build version without downloading as:

using System.Reflection;
using System.IO;

...

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

Edit : If you want to read the file, 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 ...

+4
source

You can specify the target assembly path in AssemblyName.GetAssemblyName

AssemblyName.GetAssemblyName("ProjectB.exe").Version
+3
source

, , x86 x64 .

, , , , , , , -, .

0

All Articles