8

App.Major我可以通过使用,App.Minor等在 vb6 中获取 exe 的文件版本(它自己的版本)App.Revision。但是我怎样才能在 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 的 bin 文件夹中检查了它。在 Windows 8 中,当我看到文件属性时,我没有看到任何像程序集版本这样的选项)

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

4

8 回答 8

4

使用FileVersionInfo.GetVersionInfo,例如:

Dim myFileVersionInfo As FileVersionInfo =
    FileVersionInfo.GetVersionInfo([Assembly].GetExecutingAssembly().Location)
于 2013-04-05T07:58:14.007 回答
3

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

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

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

我刚刚想出了获取 FileVersion 的代码。我发现这个问题是因为我也在寻找如何检索它(在 VB 中,而不是 C# 中)。我认为 C# 使大多数事情变得更容易,包括访问程序集。因此,这是检索程序文件版本的 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 之类的。哇。一路走好,微软!至少这个并不像他们的一些特技那么难,就像只是演奏你自己的音乐串一样。

于 2013-11-26T20:04:33.247 回答
3

实际上采用Shawns Answer并对其进行一些改进,您可以使用一行代码检索文件版本

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

或者您可以将其放在标签中

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

好样的!VB.NET 像往常一样简单....

于 2014-09-28T01:48:32.640 回答
1

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

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

等等

查看这篇文章了解程序集的描述:http: //visualbasic.about.com/od/FWTools/a/FWAssemblies.htm 作为一个快速信息:一个程序集可以包含多个文件 - 所以程序集版本比文件信息更好.

于 2013-04-05T07:58:34.080 回答
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
于 2015-01-12T16:58:18.500 回答
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需要放在 a 中Module,不能在 a 中创建Class。如果您想了解更多关于Extension Methods在上面的搜索栏中键入它的信息。他们会让你的生活更轻松。;)

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

Imports System.Runtime.CompilerServices

现在在任何程序集上,您都可以简单地调用这样的东西。

Dim Version as String = AnyAssembly.GetFileVersion()
于 2016-11-23T01:10:47.590 回答
0
Dim verstr1 As String
        OpenFileDialog1.ShowDialog()
        Dim MyFileverInfo As FileVersionInfo = FileVersionInfo.GetVersionInfo(OpenFileDialog1.FileName)
        verstr1 = MyFileverInfo.FileName & vbCrLf & MyFileverInfo.InternalName _
            & vbCrLf & MyFileverInfo.CompanyName & vbCrLf & MyFileverInfo.FileMajorPart & vbCrLf & _
            MyFileverInfo.FileMinorPart
于 2021-11-17T16:04:33.967 回答
0

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

于 2016-11-23T02:09:32.480 回答