What version of a particular dll is my application running? Find out using code.

Sometimes you may wish to find out what versions of particular dlls’ your application is using. If running on your dev machine it is not hard.

But what if you have an application that is to be deployed to hundreds of users?

Well, the easiest thing is to have your users to start your application and then check with TaskManager or even better with the extremely good and usable Process Explorer.

With this powerful tool you can get very detailed information about your processes. There is basically not a single day when I do not use it. You’ll find it here:

"Process Explorer"

https://technet.microsoft.com/en-us/sysinternals/bb896653.aspx

But this is side note, because you most likely do not want to have each and every user to manually start Process Explorer or TaskManager and report to you what versions they are using.

So, say for example that your application connects to Sql Server using the OleDbConnection classes via the SqlOleDb provider and that for some reason you would like

to know what the System.Data.dll (which contains the System.Data.OleDbConnection classes) and the SqlOleDb.dll versions are.

It is simple; you just use the Process and ProcessModule classes found in the System.Diagnostics namespace.

As usual, I’ll show this with an example. Note again that this is AS IS and is just intended to be an example to get you going.

The methods for the Process class include, for example, Kill. Which does exactly what is says on the tin. So be cautious.

Create a new .Net console application and insert this code.

        static void Main(string[] args)

        {

            // The processess we are interested of

            string[] processess = { "System.Data.dll", "sqloledb.dll" };

            String cs = String.Format(@"Provider=SQLOLEDB;Data Source=<your server>;Integrated Security=SSPI;Initial Catalog=<your database>");

            using (OleDbConnection con = new OleDbConnection(cs))

            {

                try

                {

                    //Note that the sqloledb module is not loaded until the connection is opened.

                    con.Open();

                    // Get the current process, ie. this application and the the loaded modules

                    Process currentProcess = Process.GetCurrentProcess();

                    ProcessModuleCollection pmCollection = currentProcess.Modules;

                    foreach (ProcessModule prm in pmCollection)

                    {

     // If the process of interest is loaded, do logging.

                        if (prm.ModuleName.Equals(processess[0]) || prm.ModuleName.Equals(processess[1]))

                        {

                            WriteToLog(prm);

       }

                    }

                    con.Close();

                }

                catch (Exception ex)

                {

                    Console.WriteLine(ex);

                }

            }

        }

        private static void WriteToLog(ProcessModule prm)

        {

            // Just writing to console here, but could be logfile, event viewer etc

            Console.WriteLine("Version {0} of {1} loaded...", prm.FileVersionInfo.ProductVersion, prm.ModuleName);

            // The more verbose output:

            //Console.WriteLine(pm.FileVersionInfo);

        }

This will output the following (on my Vista 64 bit machine with .Net 2.0 SP2):

Version 2.0.50727.3053 of System.Data.dll loaded...

Version 6.0.6001.18000 of sqloledb.dll loaded...

Or if you use the more verbose output:

Version 2.0.50727.3053 of System.Data.dll loaded...

File: C:\Windows\assembly\GAC_64\System.Data\2.0.0.0__b77a5c561934e089\System.Data.dll

InternalName: system.data.dll

OriginalFilename: system.data.dll

FileVersion: 2.0.50727.3053 (netfxsp.050727-3000)

FileDescription: .NET Framework

Product: Microsoft® .NET Framework

ProductVersion: 2.0.50727.3053

Debug: False

Patched: False

PreRelease: False

PrivateBuild: False

SpecialBuild: False

Language: English (United States)

Version 6.0.6001.18000 of sqloledb.dll loaded...

File: C:\Program Files\Common Files\System\Ole DB\sqloledb.dll

InternalName: sqloledb.dll

OriginalFilename: sqloledb.dll

FileVersion: 6.0.6001.18000 (longhorn_rtm.080118-1840)

FileDescription: OLE DB Provider for SQL Server

Product: Microsoft® Windows® Operating System

ProductVersion: 6.0.6001.18000

Debug: False

Patched: False

PreRelease: False

PrivateBuild: False

SpecialBuild: False

Language: English (United States)

Simple as that.

More info found here:

"NET Framework Class Library - Process Class"

https://msdn.microsoft.com/en-us/library/system.diagnostics.process(VS.80).aspx

".NET Framework Class Library - ProcessModule Class"

https://msdn.microsoft.com/en-us/library/system.diagnostics.processmodule(VS.80).aspx

".NET Framework Class Library - ProcessModuleCollection Class"

https://msdn.microsoft.com/en-us/library/system.diagnostics.processmodulecollection(VS.80).aspx