Visual Studio如何设置依赖于程序集版本的输出路径 [英] Visual studio How to set output path dependant of assembly version

查看:108
本文介绍了Visual Studio如何设置依赖于程序集版本的输出路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将主输出路径配置为

I want to configure my main output path to something like

C:\Company\UpdaterLauncher\Worker\Version

其中的版本是我的AssemblyInfo.Version字符串.

Where version is my AssemblyInfo.Version in string.

因此,每当我决定更改程序集版本时,都会新建一个文件夹.

So a new folder each time I decide to change the assembly version.

我知道我可以一直更改输出..但是很烦人.

I know I can change output all time.. But it's annoying.

是否可以在视觉输出路径中使用类似"C:\Company\UpdaterLauncher\Worker\{AssemblyVersion}"的内容来解释它并在我想要的位置构建?

Is this possible to use something like "C:\Company\UpdaterLauncher\Worker\{AssemblyVersion}" for visual output path to interprete it and build where I want?

我看了一些文档,却没有找到类似的东西...

I looked a bit in documentation and didn't found anything like this...

推荐答案

您以哪种方式构建项目?通过msbuild命令行还是在VS IDE中?

Which way do you build the project? By msbuild command-line or within VS IDE?

第一个方向:让我们先阅读程序集版本号在构建开始之前,然后 将其传递给outputpath属性.

First direction: Let's read the assembly version number before the build starts, then pass it to outputpath property.

我编写了一个脚本,试图在构建开始之前读取版本.但并非完全有效:(

I've written a script trying to read the version before the build starts. But not completely work:(

例如:以一个类库项目为例.

E.g: Using a class library project as the example.

右键单击项目,然后选择编辑xx.csproj,将脚本(从In属性添加到FourthNum属性)添加到PropertyGroup中:

Right-click the project and choose edit the xx.csproj, add the script (From In property to FourthNum property) into the PropertyGroup:

  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{DAB28A16-73AD-4EC5-9F8D-E58CE3EC84BE}</ProjectGuid>
    ......
    <In>$([System.IO.File]::ReadAllText('$(MSBuildProjectDirectory)\properties\AssemblyInfo.cs'))</In>
    <Pattern>\[assembly: AssemblyVersion\(.(\d+)\.(\d+)\.(\d+).(\d+)</Pattern>
    <FirstNum>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern),System.Text.RegularExpressions.RegexOptions.Multiline).Groups[1].Value)</FirstNum>
    <SecondNum>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern),System.Text.RegularExpressions.RegexOptions.Multiline).Groups[2].Value)</SecondNum>
    <ThirdNum>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern),System.Text.RegularExpressions.RegexOptions.Multiline).Groups[3].Value)</ThirdNum>
    <FourthNum>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern),System.Text.RegularExpressions.RegexOptions.Multiline).Groups[4].Value)</FourthNum>
  </PropertyGroup>

它将从AssemblyInfo.cs中读取程序集版本号.如果我有一个程序集版本为3.13.8.5的程序集.然后FirstNum=3, SecondNum=13 ...

It will read the assembly version number from AssemblyInfo.cs. If I have an assembly whose assembly version is 3.13.8.5. Then the FirstNum=3, SecondNum=13 ...

并将输出路径设置为:<OutputPath>C:\Company\UpdaterLauncher\Worker\$(FirstNum).$(SecondNum).$(ThirdNum).$(FourthNum)</OutputPath>

And set the outputpath as: <OutputPath>C:\Company\UpdaterLauncher\Worker\$(FirstNum).$(SecondNum).$(ThirdNum).$(FourthNum)</OutputPath>

重新加载项目并进行构建.您可以在C:\Company\UpdaterLauncher\Worker\3.13.8.5中找到构建输出.

Reload the project and build it. You can find the build output there C:\Company\UpdaterLauncher\Worker\3.13.8.5.

注意:

1.这样,因为我们将在调试和发布模式下进行构建.我们需要在两个属性组中同时设置outputpath的值以进行调试和发布.(2个地方)

1.In this way, since we will build it in both debug and release mode. We need to set the outputpath value in both propertygroup for debug and release.(2 places)

2.由于我们仅根据版本定义输出,因此调试输出和发行版均位于同一文件夹中.所以我认为<OutputPath>会更好:

2.Since we only define the output depending on version, the debug output and release will all locates in same folder. So I think the <OutputPath> would be better like:

<OutputPath>C:\Company\UpdaterLauncher\Worker\$(FirstNum).$(SecondNum).$(ThirdNum).$(FourthNum)\$(Configuration)</OutputPath>

3.在VS IDE中更改版本后,该脚本将立即不起作用.

3.This script won't work immediately after you change the version in VS IDE.

通过命令行:效果很好,每次我们更改版本号并进行构建时,输出都是正确的.

Via Command-line: It works well, every time we change the version number and build it, the output is correct.

在VS IDE中:每次更改版本后,都需要我们通过右键单击项目来卸载和重新加载项目文件,然后它才能工作.所以我说那还不是很完美.(我认为这个问题与VS何时以及如何加载项目文件有关)

Within VS IDE: Every time after we change the version, it needs us to unload and reload the project file by right-clicking the project, then it will work. So I say it isn't that perfect.(I would think this issue has something to do with when and how the VS loads the project file)

第二方向:构建输出实际上是复制相关的 程序集输出到文件夹.这样我们就可以复制或移动输出内容 复制或移动任务后,将其复制到要构建的目录 上.

Second Direction: The build output actually is copy the related assemblies to output folder. So we can copy or move the output content to the directory after the build we want by copy or move task.

我们可以使用 GetAssemblyIdentity 以在构建后获取信息.

We can check this issue, using GetAssemblyIdentity to get the info after the build.

使用上面的方法获取版本号,将其命名为$(MyVersion).然后使用构建后目标将输出复制到指定的文件夹.

Using the way above to get version number, name it $(MyVersion). Then use a after-build target to copy the output to the specified folder.

<Target Name="CopyToSpecificFolder" AfterTargets="build">
    <GetAssemblyIdentity
        AssemblyFiles="$(OutputPath)$(AssemblyName).dll">
      <Output
          TaskParameter="Assemblies"
          ItemName="MyAssemblyIdentities"/>
    </GetAssemblyIdentity>
    <PropertyGroup>
      <MyVersion>%(MyAssemblyIdentities.Version)</MyVersion>
    </PropertyGroup>
    <ItemGroup>
      <Out Include="$(OutputPath)*.*" />
      </ItemGroup>
    <Copy DestinationFolder="C:\Company\UpdaterLauncher\Worker\$(MyVersion)" SourceFiles="@(Out)"/>
  </Target>

将此脚本添加到xx.csproj文件中.在它的底部,像:

Add this script into the xx.csproj file. In the bottom of it like:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  ......

  <Target Name="CopyToSpecificFolder" AfterTargets="build">
    ......
  </Target>
</Project>

无论是VS IDE还是命令行,它都能很好地工作.这是针对类项目的,如果您正在开发一个.exe项目,请将$(AssemblyName).dll更改为$(AssemblyName).exe.

It works well in whether VS IDE or by command-line. And it's for class project, if you're developing a .exe project, change the $(AssemblyName).dll to $(AssemblyName).exe.

这篇关于Visual Studio如何设置依赖于程序集版本的输出路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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