获取文件版本

时间:2013-04-05 07:46:17

标签: .net vb.net

我可以使用App.MajorApp.MinorApp.Revision等在vb6中获取exe文件版本(自己的版本)等。但是我怎样才能在vb.net中获得相同的内容。我尝试使用以下3种方法。

Text1.Text = My.Application.Info.Version.Major & "." & My.Application.Info.Version.Minor & "." & My.Application.Info.Version.Build & "." & My.Application.Info.Version.Revision

Text2.Text = Me.GetType.Assembly.GetName.Version.ToString()

Text3.Text = My.Application.Info.Version.ToString()

在所有3个案例中,它返回汇编版本(我在一个xp机器中创建的exe文件夹中检查了它。在Windows 8中,当我看到文件时,我没有看到任何选项,如汇编版本属性)

默认情况下,程序集和文件版本都相同。但是当我在project properties->applicationassembly information->File version中手动更改它时,我开始知道我的代码正在返回程序集版本。 那我怎么能得到文件版本?什么是装配和文件vesrion?

7 个答案:

答案 0 :(得分:3)

所以在.NET程序中,实际上有两个版本,而不仅仅是一个版本(我知道)。汇编版本是您正在抓取的,并且更容易在.NET中检索。它本质上是“.NET的版本”,所以当Assemblies有不同的版本时,就是它使用的版本。

然后有“文件版本”或在一个地方,我看到微软甚至称它为“汇编文件版本”,使混乱更加严重。所以真正需要查看名称是否包含“文件”。程序集的“文件”版本与文件系统相关联,因此当您在Windows资源管理器中查看该版本时,您将获得此版本。

为什么微软把它们分成两个不同的东西,并没有把它们绑在一起,我不明白。也许有人可以进一步启发我。

我刚想出了获取FileVersion的代码。我发现了这个问题,因为我一直在寻找如何检索它(在VB中,而不是C#)。我认为C#使大多数事情变得更容易,包括访问大会。所以这是用于检索程序的 File 版本的VB代码:

Dim fileName$ = System.Reflection.Assembly.GetExecutingAssembly().Location
Dim fileName2$ = Application.ExecutablePath ' this grabs the filename too,
' but #2 ends with ".EXE" while the 1st ends with ".exe" in my Windows 7.

' either fileName or fileName2 works for me.
Dim fvi As FileVersionInfo = FileVersionInfo.GetVersionInfo(fileName)
' now this fvi has all the properties for the FileVersion information.
Dim fvAsString$ = fvi.FileVersion ' but other useful properties exist too.
哇,很多代码应该像VB6中那样简单。甚至可能是App.Version.ToString等等。哇。要走的路,微软!至少这一个并不像他们的一些特技一样困难,比如只是演奏自己的音乐串。

答案 1 :(得分:2)

使用FileVersionInfo.GetVersionInfo,例如:

Dim myFileVersionInfo As FileVersionInfo =
    FileVersionInfo.GetVersionInfo([Assembly].GetExecutingAssembly().Location)

答案 2 :(得分:2)

实际上采取Shawns Answer并稍微改进一下,你可以用一行代码检索文件版本

Dim FileVer As String = FileVersionInfo.GetVersionInfo(Application.ExecutablePath).FileVersion

或者您可以将其放在标签

Me.Label1.Text = FileVersionInfo.GetVersionInfo(Application.ExecutablePath).FileVersion

路要走! VB.NET像往常一样简单....

答案 3 :(得分:1)

获取应用程序版本的最简单方法是:

    My.Application.Info.Version.Major
    My.Application.Info.Version.MajorRevision

查看本文以获取程序集的描述: http://visualbasic.about.com/od/FWTools/a/FWAssemblies.htm 作为一个qucik信息:程序集可以包含多个文件 - 因此assemblyversion优于文件信息。

答案 4 :(得分:0)

以下是从程序集本身获取程序集文件版本信息的更可靠方法:

Function GetAssemblyFileVersion() As String
    Dim ProjectAssembly As Reflection.Assembly 
    ProjectAssembly = Reflection.Assembly.GetExecutingAssembly()

    For Each attr As Attribute In ProjectAssembly.GetCustomAttributes(False)
        If TypeOf attr Is Reflection.AssemblyFileVersionAttribute Then
            Dim FileVerAttr As Reflection.AssemblyFileVersionAttribute = attr
            Return FileVerAttr.Version
        End If
    Next

    Throw New Reflection.TargetInvocationException(
        New KeyNotFoundException(
            "Cannot find custom attribute 'AssemblyFileVersionAttribute'"))
End Function

答案 5 :(得分:0)

这是我创建的用于返回AssemblyFileVersion的扩展方法。 此方法适用于应用程序和类库。

''' <summary>
''' Gets the AssemblyFileVersion from the specified assembly.
''' </summary>
''' <param name="Assembly">[Assembly] Assembly to get the Assembly File Version from.</param>
''' <returns>[String] The Assembly's file version.</returns>
<Extension>
Friend Function GetFileVersion(ByVal Assembly As Assembly) As String
    On Error GoTo Err
    Return DirectCast(Assembly.GetCustomAttribute(GetType(AssemblyFileVersionAttribute)), AssemblyFileVersionAttribute).Version
Err:
    Return Nothing
End Function

请注意,Extension Methods需要放在Module中,不能在Class中创建。如果您想了解有关Extension Methods的更多信息,请在上面的搜索栏中输入。它们会让你的生活更轻松。 ;)

确保在模块中包含以下内容...

Imports System.Runtime.CompilerServices

现在在任何装配上你都可以简单地调用这样的东西。

Dim Version as String = AnyAssembly.GetFileVersion()

答案 6 :(得分:0)

我已经烧光了,但你试过Application.ProductVersion吗?我在其他问题中没有看到它。这将返回x.x.x.x项目设置上的任何版本。请注意,对于clickonce,您需要从其他站点获取它。