Monday, February 16, 2009

Identify Assembly Version with C#

With Assembly, we can get some informations, there are:

    • AssemblyTitleAttribute

    • AssemblyCompanyAttribute

    • AssemblyFileVersionAttribute

    • AssemblyProductAttribute

    • AssemblyCopyrightAttribute

    • AssemblyDescriptionAttribute

    • ETC

    For completed list, use this:

       1: Assembly executingAssembly = Assembly.GetExecutingAssembly();
       2: object[] customAttributes = executingAssembly.GetCustomAttributes(false);
    Below are the sample code, to identify similar DLL but with different version.

       1: Dictionary<string, string> dllFileNames = new Dictionary<string, string>();
       2: string directory = @"D:\Inetpub\wwwroot\Windows.Test\bin";
       3: DirectoryInfo directoryInfo = new DirectoryInfo(directory);
       4: if (directoryInfo != null)
       5: {
       6:     foreach (FileInfo fileInfo in directoryInfo.GetFiles("*.dll", SearchOption.AllDirectories))
       7:     {
       8:         if (!dllFileNames.ContainsKey(fileInfo.Name))
       9:             dllFileNames.Add(fileInfo.Name, fileInfo.FullName);
      10:         else
      11:         {
      12:             int assembly1Version = 0;
      13:             Assembly assembly1 = Assembly.LoadFile(dllFileNames[fileInfo.Name]);
      14:             object[] customAttributes = assembly1.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false);
      15:             if (customAttributes.Length > 0)
      16:             {
      17:                 string version = ((AssemblyFileVersionAttribute)customAttributes[0]).Version;
      18:                 //Parse string version to int version
      19:                 //  ex: 1.0.0.1 >> 1001
      20:                 assembly1Version = Program.VersionToInt(version);
      21:             }
      22:  
      23:             int assembly2Version = 0;
      24:             Assembly assembly2 = Assembly.LoadFile(fileInfo.FullName);
      25:             customAttributes = assembly2.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false);
      26:             if (customAttributes.Length > 0)
      27:             {
      28:                 string version = ((AssemblyFileVersionAttribute)customAttributes[0]).Version;
      29:                 //Parse string version to int version
      30:                 //  ex: 1.0.0.1 >> 1001
      31:                 assembly2Version = Program.VersionToInt(version);
      32:             }
      33:  
      34:             if (assembly2Version > assembly1Version)
      35:             {
      36:                 dllFileNames.Remove(fileInfo.Name);
      37:                 dllFileNames.Add(fileInfo.Name, fileInfo.FullName);
      38:             }
      39:         }
      40:     }
      41:  
      42:     foreach (string dllFileName in dllFileNames.Keys)
      43:     {
      44:         string fullName = dllFileNames[dllFileName];
      45:         //Some methods
      46:     }
      47: }
    Have fun!

    No comments:

    Post a Comment