How to automatically insert version number into AssemblyName

瘦欲@ 提交于 2019-12-06 02:18:25

The Target Name is is shown on the General page under the Configuration Properties tab in the IDE Property Page editor. I don't have one handy myself to look up the name for you, but you can do it by changing the blank in the IDE to something like XXXX and save. Then view the diff in the version control commit reviewer and see what the name of the Property is. In this case, then edit the line to change XXXX to $(OutputPath)$(AssemblyName).v$(VersionNumberShort)

Oh, check out the FormatVersion task, which might help. I think there are some premade tasks that manipulate a version assembly similar to what you show, too.

What I'm doing for versions is passing the pieces in via #defines as /D command line arguments. I guess you don't have that in C# though, IIRC.

This works for me, and it solves the seemingly simple problem of appending the version info string to a filename at build.

First, the post-build event:

(Right-Click Project -> Properties -> Build Events -> Edit Post-build...)

$(TargetPath) "version" > $(TargetDir)text.txt
set /p version= <$(TargetDir)text.txt
copy $(TargetPath) $(TargetDir)$(TargetName)_%version%.exe
del $(TargetDir)text.txt

Now, the trick: Overload sub main to return the version info, and call it in a post-build event on the exe that was just built.

here is an example in F#:

[<EntryPoint>]
let main argv = 

    let version = argv.Length = 1 && argv.[0] = "version"
    if version then
        let version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()
        do stdout.WriteLine(version)
        do stdout.Flush()
    else 
        try
        //...

The post-build event above

1) calls the newly built exe with a "version" arg, and writes the output to a txt file

2) reads the text file contents into a local variable

3) renames the newly built exe by adding the version info

3) copies the newly built exe adding the version info to the name

4) cleans up the temp file

*changed "move" to "copy" so that Visual Studio can still F5 the project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!