Solved

Getting running version info from another assembly

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now