使用 MSI 设置版本设置 AssemblyInfo 版本号

问题描述 投票:0回答:2

我正在使用安装项目来发布我的项目。我希望每个项目的版本与安装版本相同。

我想在 Visual Studio 中更改我的安装版本属性,构建后,所有项目版本都从该属性更新,这可能吗?

c# .net visual-studio-2010 windows-installer versioning
2个回答
46
投票

项目有程序集和文件版本号:(不是设置版本,我相应地编辑了您的问题) enter image description here

答案1:

如果您想让安装项目版本号设置程序集和文件版本号,您需要使用由构建触发的脚本/exe 来完成此操作。

enter image description here

这篇关于如何自动更新程序集版本号的文章显示了一半的解决方案......

根据我所做的研究,不可能在 PreBuildEvent 中使用 SetupVersion。没有适用于它的 $SetupVersion 命令:http://msdn.microsoft.com/en-us/library/42x5kfw4(v=vs.80).aspx

必须使用 -set: 命令更改每个构建的 PreBuildEvent(如代码项目文章中的

此评论
所示)并不理想。

我们需要的解决方案是一个 PreBuildEvent 来调用 AssemblyInfoUtil.exe 并让它从 vdproj 项目文件中读取“ProductVersion”。然后更新程序集版本号。

我修改了文章中的代码,以向您展示如何从 Setup.vdproj 读取产品版本,这就是如何从 PreBuildEvent 调用它:

AssemblyInfoUtil.exe -setup:"C:\Program Files\MyProject1\Setup1\Setup1.vdproj" -ass:"C:\Program Files\MyProject1\AssemblyInfo.cs"

这是修改后的代码:

using System;
using System.IO;
using System.Text;

namespace AssemblyInfoUtil
{
    class AssemblyInfoUtil
    {
    private static int incParamNum = 0;    
    private static string fileName = "";  
    private static string setupfileName = "";       
    private static string versionStr = null;    
    private static bool isVB = false;
    [STAThread]
    static void Main(string[] args)
    {
        for (int i = 0; i < args.Length; i++) {
            if (args[i].StartsWith("-setup:")) {
           string s = args[i].Substring("-setup:".Length);
           setupfileName = int.Parse(s);
            }
            else if (args[i].StartsWith("-ass:")) {
           fileName = args[i].Substring("-ass:".Length);
            }
        }

        //Jeremy Thompson showing how to detect "ProductVersion" = "8:1.0.0" in vdproj
        string setupproj = System.IO.File.ReadAllText(setupfileName);
        int startPosOfProductVersion = setupproj.IndexOf("\"ProductVersion\" = \"") +20;
        int endPosOfProductVersion = setupproj.IndexOf(Environment.NewLine, startPosOfProductVersion) - startPosOfProductVersion;
        string versionStr = setupproj.Substring(startPosOfProductVersion, endPosOfProductVersion);
        versionStr = versionStr.Replace("\"", string.Empty).Replace("8:",string.Empty);
      
        if (Path.GetExtension(fileName).ToLower() == ".vb")
        isVB = true;

        if (fileName == "") {
        System.Console.WriteLine("Usage: AssemblyInfoUtil 
           <path to :Setup.vdproj file> and  <path to AssemblyInfo.cs or AssemblyInfo.vb file> [options]");
        System.Console.WriteLine("Options: ");
        System.Console.WriteLine("  -setup:Setup.vdproj file path");
        System.Console.WriteLine("  -ass:Assembly file path");
        return;
        }

        if (!File.Exists(fileName)) {
        System.Console.WriteLine
            ("Error: Can not find file \"" + fileName + "\"");
        return;
        }

        System.Console.Write("Processing \"" + fileName + "\"...");
        StreamReader reader = new StreamReader(fileName);
             StreamWriter writer = new StreamWriter(fileName + ".out");
        String line;

        while ((line = reader.ReadLine()) != null) {
        line = ProcessLine(line);
        writer.WriteLine(line);
        }
        reader.Close();
        writer.Close();

        File.Delete(fileName);
        File.Move(fileName + ".out", fileName);
        System.Console.WriteLine("Done!");
    }

    private static string ProcessLine(string line) {
        if (isVB) {
        line = ProcessLinePart(line, "<Assembly: AssemblyVersion(\"");
        line = ProcessLinePart(line, "<Assembly: AssemblyFileVersion(\"");
        } 
        else {
        line = ProcessLinePart(line, "[assembly: AssemblyVersion(\"");
        line = ProcessLinePart(line, "[assembly: AssemblyFileVersion(\"");
        }
        return line;
    }

    private static string ProcessLinePart(string line, string part) {
        int spos = line.IndexOf(part);
        if (spos >= 0) {
        spos += part.Length;
        int epos = line.IndexOf('"', spos);
        string oldVersion = line.Substring(spos, epos - spos);
        string newVersion = "";
        bool performChange = false;

        if (incParamNum > 0) {
            string[] nums = oldVersion.Split('.');
            if (nums.Length >= incParamNum && nums[incParamNum - 1] != "*") {
            Int64 val = Int64.Parse(nums[incParamNum - 1]);
            val++;
            nums[incParamNum - 1] = val.ToString();
            newVersion = nums[0]; 
            for (int i = 1; i < nums.Length; i++) {
                newVersion += "." + nums[i];
            }
            performChange = true;
            }
        }
        
        else if (versionStr != null) {
            newVersion = versionStr;
            performChange = true;
        }

        if (performChange) {
            StringBuilder str = new StringBuilder(line);
            str.Remove(spos, epos - spos);
            str.Insert(spos, newVersion);
            line = str.ToString();
        }
        } 
        return line;
    }
     }
}

答案2:

以我的思维方式,更好的方法是使用 Shared Assembly Info 类而不是单独的 AssemblyInfo 类文件。

要实现此目的,请在解决方案文件夹中创建一个名为 SharedAssemblyInfo.cs 的文件,然后在每个项目中添加一个指向 SharedAssemblyInfo.cs 的链接。您还可以将链接的 SharedAssemblyInfo.cs 移动到 Properties 文件夹中,以便它与解决方案中特定于每个项目的 AssemblyInfo.cs 并排放置,如下所示。

enter image description here

这是一个示例 SharedAssemblyInfo.cs 文件:

using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following 
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyCompany("Saint Bart Technologies")]
[assembly: AssemblyProduct("Demo")]
[assembly: AssemblyCopyright("Copyright ? Saint Bart 2013")]
[assembly: AssemblyTrademark("")]

// Make it easy to distinguish Debug and Release (i.e. Retail) builds;
// for example, through the file properties window.
#if DEBUG
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Flavor=Debug")] // a.k.a. "Comments"
#else
[assembly: AssemblyConfiguration("Retail")]
[assembly: AssemblyDescription("Flavor=Retail")] // a.k.a. "Comments"
#endif

[assembly: CLSCompliant(true)]

// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components.  If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// Note that the assembly version does not get incremented for every build
// to avoid problems with assembly binding (or requiring a policy or
// <bindingRedirect> in the config file).
//
// The AssemblyFileVersionAttribute is incremented with every build in order
// to distinguish one build from another. AssemblyFileVersion is specified
// in AssemblyVersionInfo.cs so that it can be easily incremented by the
// automated build process.
[assembly: AssemblyVersion("1.0.0.0")]

// By default, the "Product version" shown in the file properties window is
// the same as the value specified for AssemblyFileVersionAttribute.
// Set AssemblyInformationalVersionAttribute to be the same as
// AssemblyVersionAttribute so that the "Product version" in the file
// properties window matches the version displayed in the GAC shell extension.
[assembly: AssemblyInformationalVersion("1.0.0.0")] // a.k.a. "Product version"

这是一个示例 AssemblyInfo.cs 文件:

// Note: Shared assembly information is specified in SharedAssemblyInfo.cs
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following 
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("WindowsFormsApplication2")]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("ffded14d-6c95-440b-a45d-e1f502476539")]

因此,每次您想要更改所有项目装配信息时,您都可以在一处完成。我假设您希望将 MSI 设置版本设置为与程序集版本号相同,这是一个手动步骤。


答案3:

考虑改用MSBuild它有所有这些好处,但我不确定你现在是否有时间接受它。


答案4:

程序集可以使用 AssemblyInfo.cs 中的以下星号语法自动递增其内部版本号:

[assembly: AssemblyVersion("1.0.0.*")]

这是一个很好的方法,因为跟踪内部版本号的目的是 能够识别不同的构建。进行预构建更改 由于构建尚未发生,因此构建编号无法实现此目的。


答案5:

此处的另一个 CodeProject 答案假设您要更新安装 MSI 项目文件中的

 ProductVersion, ProductCode, PackageCode
。我没有那样解释你的问题,根据这个线程,存在问题: 用于更改安装项目的 ProductVersion 的预构建事件只有在构建之后才会生效


答案6(新):

有一些 TFS Build 插件可以设置“Assembly Info”:https://marketplace.visualstudio.com/items?itemName=bleddynrichards.Assembly-Info-Task https://marketplace.visualstudio.com/items?itemName=bool.update- assembly-info https://marketplace.visualstudio.com/items?itemName=ggarbuglia.set assemblyversion-task


答案 7 - .Net Core:

从 GlobalAssemblyInfo.cs 移至 Directory.Build.props:

旧的

GlobalAssemblyInfo.cs
文件通常包含跨项目共享的属性的重复代码。切换到
Directory.Build.props
可以集中这些设置,而无需直接修改 C# 代码。

在解决方案的根目录(.sln 文件所在的位置)中,创建一个名为 Directory.Build.props 的文件,其中包含以下内容:

<?xml version="1.0" encoding="utf-8" ?>
<Project>
  <PropertyGroup>
    <!-- Assembly versioning -->
    <Version>1.0.0.0</Version>
    <FileVersion>1.0.0.0</FileVersion>
    <AssemblyVersion>1.0.0.0</AssemblyVersion>
    <Company>YourCompany</Company>
    <Product>YourProduct</Product>
  </PropertyGroup>
</Project>

2
投票

我不知道这是否可以完美解决您的问题,但您可以使用所有配置管理信息实现一个公共类,例如:

public class VersionInfo{
    public const string cProductVersion = "1.0.0"
    //other version info
}

您可以使用新类更新所有 AssemblyInfo.cs 后:

[assembly: AssemblyVersion(VersionInfo.cProductVersion)]

我希望这有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.