Solved

Getting running version info from another assembly

Posted on 2014-04-14
3
208 Views
Last Modified: 2014-04-20
I'm using this code to give me version info and build date-time (using "assembly: AssemblyVersion("1.1.*")]"). I'd like to be able to get this info with a call from a different assembly, so that it will give me the info for that assembly. How can I do that?
public static class VersionInfo {
        /// <summary>
        /// Current build Major.Minor version info
        /// </summary>
        public static string Version {
            get { return RunningVersion.Major + "." + RunningVersion.Minor; }
        }
        /// <summary>
        /// Current build DateTime
        /// </summary>
        public static DateTime BuildDateTime {
            get {
                return new DateTime(2000, 1, 1).Add(
                   new TimeSpan(TimeSpan.TicksPerDay * RunningVersion.Build + // Days since 1 Jan 2000
                       TimeSpan.TicksPerSecond * 2 * RunningVersion.Revision));    // Seconds since midnight
            }
        }
        private static readonly Version RunningVersion = Assembly.GetExecutingAssembly().GetName().Version;
    }

Open in new window

0
Comment
Question by:BlearyEye
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39999348
Use the AppDomain class:

e.g.

Version v = AppDomain.CurrentDomain.GetAssemblies()
                                   .Where(a => a.FullName.StartsWith("ClassLibrary1"))
                                   .First()
                                   .GetName()
                                   .Version;

Open in new window


Be aware that GetAssemblies returns the assemblies that have actually been loaded. You may have an assembly being used by your project that hasn't yet been loaded by the runtime at the point in which you call GetAssemblies. If there is a chance that you have this going on, then you need to either cause the assembly to be loaded--by using some of its types--or by explicitly loading the assembly into the AppDomain.
0
 
LVL 1

Assisted Solution

by:BlearyEye
BlearyEye earned 0 total points
ID: 39999418
I may have come up with a simpler approach:
    public static class VersionInfo {

        /// <summary>
        /// The version for which info is required
        /// </summary>
        private static Version _theVersion = Assembly.GetExecutingAssembly().GetName().Version;

        /// <summary>
        /// Initialize the version
        /// </summary>
        /// <remarks>Skip this if you want version info from the assembly in which this class resides</remarks>
        /// <param name="theAssembly"></param>
        public static void Init(Assembly theAssembly) {
            _theVersion = theAssembly.GetName().Version;
        }

        /// <summary>
        /// Current build Major.Minor version info
        /// </summary>
        public static string Version {
            get { return _theVersion.Major + "." + _theVersion.Minor; }
        }


        /// <summary>
        /// Current build DateTime
        /// </summary>
        public static DateTime BuildDateTime {
            get {
                return new DateTime(2000, 1, 1).Add(
                   new TimeSpan(TimeSpan.TicksPerDay * _theVersion.Build + // Days since 1 Jan 2000
                       TimeSpan.TicksPerSecond * 2 * _theVersion.Revision));    // Seconds since midnight
            }
        }

    }

Open in new window

I can call Init first:
VersionInfo.Init(Assembly.GetExecutingAssembly());

Open in new window

After than I can get version info.

Your solution is more general and would work for any assembly in the solution.
0
 
LVL 1

Author Closing Comment

by:BlearyEye
ID: 40011188
kaufmed gave the general solution. I needed something more direct, so used included my solution.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Example works but why do I need to click twice on button 4 48
C# Gridview 1 47
Create a WebAPI using a generic return parameter 9 30
Input parameteres to DragOver 2 19
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question