Solved

Getting running version info from another assembly

Posted on 2014-04-14
3
207 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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