Friday, February 4, 2011

Powershell to find version of .net assemblies

Las week we upgraded our application version from 2010 to 2011.This involved a lot of changes in the configuration files along with UI changes such as logos ,images etc...One of the change was in the assembly version and correction of config files to reflect the version. Most of our code is relying on .net reflection so the config files should be updated properly to get the fully qualified name of classes.

Till then everything was fine. We got problems when the build came. The build script failed to build the dlls in the new assembly version. The product have around 400 DLLs and we are not using GAC. How to find out which dll is in wrong version?

One solution is to put the assemblies into GAC and see the version.Another is to open the DLL in reflector and check the version. Since the number of DLLs is somewhat huge ,it is better to write a program to find out the assemblies which are in wrong version.The next question came C# or VB.Net?

But keeping mind the principle of architecture finally decided to write a small windows power shell to find out the version.Code below.

Windows Power shell code to find out the .Net assembly / .dll version

#--------Script to find out .net assembly version mismatches---------
$curDir=$("C:\Binaries")
$pattern=($curDir + $("\*.dll"))
Foreach ($file in Get-Childitem $pattern)
{
$fp=($curDir+"\"+$file.name)

#Load the assembly
$asm=[Reflection.Assembly]::LoadFrom($fp)
$asmName=$asm.GetName()
$ver=$asmName.Version

#Create required version object to compare
$reqVer=new-object Version -arg "
4.0.0.0"

#if the version is not matching show a warning
if($ver -ne $reqVer) {
Write-Warning $fp
Write-Warning $ver.ToString(4)
}
}

No comments: