将AssemblyInfo.cs中的程序集版本放入Web.config中 [英] Put Assembly Version from AssemblyInfo.cs in Web.config

查看:58
本文介绍了将AssemblyInfo.cs中的程序集版本放入Web.config中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种情况下,我具有AssemblyInfo.cs中项目的程序集版本,例如,如下所示的2.0.0:

I have this scenario, where I have the project's Assembly Version from AssemblyInfo.cs, for example 2.0.0 as below:

[assembly: AssemblyVersion("2.0.0")]

此外,我想检索此AssemblyVersion并将其动态设置为我的web.config中参数的一部分,例如,如下所示:

In addition, I would like to retrieve this AssemblyVersion and set it as part of the parameters in my web.config dynamically, such as below, for example:

<my.config.group>
    <version versionNumber="2.0.0" />
</my.config.group>

我需要将上面的版本号设置为我的Assembly的版本号,以避免在发生某些更改时必须手动对其进行修改.

I need to set the version number above, as the version number of my Assembly dynamically, to avoid having to modify it manually when something changes.

这是必需的,因为我们使用的第三方库是如何工作的.因此,不可能使用另一种方法来获取程序集版本.我们必须坚持使用web.config.

This is required because of how a third party library we are using works. Therefore, it is not possible to fetch the assembly version using an alternative way. We must stick to the web.config.

最简单的解决方案是什么?我当时在考虑使用Powershell脚本Post-Build,但是我对Powershell的经验很少,所以我想知道什么是最佳解决方案.

What is the neatest solution to achieve this? I was thinking of using a Powershell script Post-Build, however, I have little experience in Powershell so I wanted to know what is the best solution.

推荐答案

是的,您可以使用构建后事件来做到这一点,但也可以将其作为部署步骤或任何其他步骤来实现.

Yes, you could do that using a post build event, but you can also do it as a deployment step or whatsoever.

以下脚本加载程序集,读取版本信息并格式化字符串.然后,如果不存在,它将在配置文件上创建版本节点,并设置versionNumber属性:

The following script loads the assembly, reads the version information and formats a string. Then, it creates the version node on the config file if not present and sets the versionNumber attribute:

$assemblyFilePath = "C:\YourAssembly.dll"
$configPath = "C:\YourWeb.config"

# Retrieve the version
$bytes = [System.IO.File]::ReadAllBytes($assemblyFilePath)
$assembly = [System.Reflection.Assembly]::Load($bytes)
$version = $assembly.GetName().Version;
$versionString = [string]::Format("{0}.{1}.{2}",$version.Major, $version.Minor, $version.Build)

$config = [xml](gc $configPath)
$versionNode = $config.SelectSingleNode('configuration//version')
# Ensure the version node is present
if (-not $versionNode)
{
    $versionNode = $config.CreateElement('version')
    $config.DocumentElement.AppendChild($versionNode)
}
# Set the version number attribute
$versionNode.SetAttribute('versionNumber', $versionString)
$config.Save($configPath)

您还可以从 AssemblyInfo.cs 中读取并解析 AssemblyVersion 属性.我之所以喜欢加载程序集本身,是因为即使只有二进制文件,也可以使用此解决方案.

You could also read and parse the AssemblyVersion attribute from the AssemblyInfo.cs. The reason why I prefer to load the assembly itselfs is because you can use this solution even if you only have the binaries.

这是一个示例,您如何从 AssemblyInfo.cs 中读取版本:

And here an example how you can read the version from the AssemblyInfo.cs:

$assemblyInfoPath = "C:\AssemblyInfo.cs"

$regex = '^\[assembly: AssemblyVersion\("(.*?)"\)\]'
$assemblyInfo = Get-Content $assemblyInfoPath -Raw

$version = [Regex]::Match(
        $assemblyInfo, 
        $regex,
        [System.Text.RegularExpressions.RegexOptions]::Multiline
    ).Groups[1].Value

现在,您可以使用 [version] :: Parse($ version)解析版本,并使用与上面的示例相同的格式字符串.

Now you can parse the version using [version]::Parse($version)and use the same format string as the example above.

这篇关于将AssemblyInfo.cs中的程序集版本放入Web.config中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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