.net 获取文件版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15828509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:12:00  来源:igfitidea点击:

Get file version

.netvb.net

提问by IT researcher

I can get file version of exe(its own version) in vb6 by using App.Major,App.Minor, App.Revisionetc. But how can i get the same in vb.net. I tried using following 3 methods.

我可以通过使用VB6获得EXE文件的版本(它自己的版本)App.MajorApp.MinorApp.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()

In all 3 cases it was returning assembly version(I checked it in bin folder where the exe created in a xp machine.In windows 8 i didnt see any option like assembly version when i see file properties)

在所有 3 种情况下,它都返回了程序集版本(我在 xp 机器中创建的 exe 的 bin 文件夹中检查了它。在 Windows 8 中,当我看到文件属性时,我没有看到任何像程序集版本这样的选项)

By default both assembly and file versions are same.But when i changed it manually in project properties->applicationassembly information->File versioni came to know my code is returning assembly version. So how can i get the file version? What is assembly and file vesrion?

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

回答by Martin Prikryl

Use FileVersionInfo.GetVersionInfo, for example:

使用FileVersionInfo.GetVersionInfo,例如:

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

回答by Shawn Kovac

so in .NET programs, there are really two versions, not just one (of which i'm aware). the Assembly Version is what you are grabbing and is easier to retrieve in .NET. It is essentially ".NET's version" so when Assemblies have different versions, that's the one it uses.

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

then there is the "File Version" or in one place i see Microsoft has even called it "Assembly File Version" to make the confusion even worse. so one really needs to see whether the name includes "file" or not. this "File" version for an assembly is associated with the file system, so when you look at the version in Windows Explorer, this is what you'll get.

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

why Microsoft split those as two different things and didn't tie them together, i do not understand. maybe someone can enlighten me further.

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

i just figured out the code to grab the FileVersion. i found this question because i was looking for how to retrieve it too (in VB, not C#). I think C# makes most things easier, including accessing the Assembly. So here's the VB code to retrieve the Fileversion of your program:

我刚刚想出了获取文件版本的代码。我发现这个问题是因为我也在寻找如何检索它(在 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.

wow, lots of code for something that should be something as simple as it was in VB6. Maybe even App.Version.ToString or something. wow. way to go, Microsoft! at least this one isn't as difficult as some of their stunts tho, like just playing your own music string.

哇,很多代码都应该像在 VB6 中一样简单。甚至可能是 App.Version.ToString 之类的。哇。一路走好,微软!至少这个不像他们的一些特技那么难,比如只是演奏你自己的音乐弦。

回答by 3vts

Actually taking Shawns Answer and improving it a little bit you can retrieve the file version with one line of code

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

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

or you can place it in a label

或者你可以把它放在一个标签中

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

Way to go! VB.NET as simple as usual....

加油!VB.NET 像往常一样简单....

回答by Christian Sauer

The easiest way to get the application version is this:

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

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

etc.

等等。

Look at this article for a description of assemblies: http://visualbasic.about.com/od/FWTools/a/FWAssemblies.htmAs a qucik info: An assembly can contain multiple file - so the assemblyversion is better than the file info.

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

回答by mwolfe02

The following is a more reliable way of getting the assembly's file version info from within the assembly itself:

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

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

回答by Fütemire

This is an extension method that I created to return the AssemblyFileVersion. This method will work for applications and class libraries.

这是我创建的一个扩展方法,用于返回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

Remember, Extension Methodsneed to be placed in a Module, they can not be created in a Class. If you want to learn more about Extension Methodstype it in the search bar above. They will make your life easier. ;)

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

Make sure to include the following in your Module...

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

Imports System.Runtime.CompilerServices

Now on any assembly you can simply call something like this.

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

Dim Version as String = AnyAssembly.GetFileVersion()

回答by Esselans

I'm burned out but did you try Application.ProductVersion? I don't see it in the other questions. This will return x.x.x.x whatever version is on the project settings. Note that for clickonce you need to get it from other site.

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