从AssemblyInfo.cs文件读取版本号 [英] Reading the Version number from a AssemblyInfo.cs file

查看:194
本文介绍了从AssemblyInfo.cs文件读取版本号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从AssemblyInfo.cs文件中提取版本号!
并且我试图使用 System.Reflection.Assembly.LoadFile(path); 但是这样做的时候我得到了BadImageFormatException; 该模块应包含程序集清单。(HRESULT的异常:0x80131018) 。所以现在我受伤了,这不是可行的方法吗?我应该用RegEx代替吗?

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?

我已经阅读了许多带有 GetExecutingAssembly()的示例,但我确实想获得

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

说明:我想从 AssemblyInfo.cs 文件中读取版本信息!并且不是来自已编译文件。我试图制作一个在发布新版本之前更新版本号的工具。

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.

推荐答案

您可以获取不加载的程序集版本

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);
                }

            }

//输出

1.0.*
1.0.0.0

希望获得帮助...

这篇关于从AssemblyInfo.cs文件读取版本号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆