views:

28

answers:

2

I am using C# and I wish to get the versions of some of the dll's that are in the references of my projects. I know that I can get it simply by assuming that the file is in the current folder, however, this may not always be the case. Is there a more robust way of doing this?

A: 

You can use the following code to obtain the location of the assembly witch contains the specified class

System.Reflection.Assembly.GetAssembly(typeof(SpecifiedClass)).Location
peter
I am interested in the specific dll's within the references list of the assembly.
sbenderli
So you try to check the version or location for each reference in your project?
peter
Yes, as in 0A0D's example
sbenderli
+3  A: 

Getting File Version Information

For example,

AssemblyName[] asmNames = asm.GetReferencedAssemblies();
foreach (AssemblyName nm in asmNames)
{
    Console.WriteLine(nm.FullName);
}


private bool GetReferenceAssembly(Assembly asm)
      {
               try
               {
                   AssemblyName[] list = asm.GetReferencedAssemblies();
                   if (list.Length > 0)
                   {
                       AssemblyInformation info = null;
                       _lstReferences = new List();
                        for (int i = 0; i < list.Length; i++)
                        {
                            info = new AssemblyInformation();
                            info.Name = list[i].Name;
                            info.Version = list[i].Version.ToString();
                            info.FullName = list[i].ToString();

                            this._lstReferences.Add(info);
                        }
                    }
                }
                catch (Exception err)
                {
                    this._errMsg = err.Message;
                    return false;
                }

                return true;
       }
0A0D