How can I get the executing assembly version?

前端 未结 6 1585
慢半拍i
慢半拍i 2020-12-07 13:51

I am trying to get the executing assembly version in C# 3.0 using the following code:

var assemblyFullName = Assembly.GetExecutingAssembly().FullName;
var ve         


        
6条回答
  •  再見小時候
    2020-12-07 13:55

    Two options... regardless of application type you can always invoke:

    Assembly.GetExecutingAssembly().GetName().Version
    

    If a Windows Forms application, you can always access via application if looking specifically for product version.

    Application.ProductVersion
    

    Using GetExecutingAssembly for an assembly reference is not always an option. As such, I personally find it useful to create a static helper class in projects where I may need to reference the underlying assembly or assembly version:

    // A sample assembly reference class that would exist in the `Core` project.
    public static class CoreAssembly
    {
        public static readonly Assembly Reference = typeof(CoreAssembly).Assembly;
        public static readonly Version Version = Reference.GetName().Version;
    }
    

    Then I can cleanly reference CoreAssembly.Version in my code as required.

提交回复
热议问题