How do I get the version of an assembly without loading it?

后端 未结 4 861
故里飘歌
故里飘歌 2020-12-01 17:53

One small function of a large program examines assemblies in a folder and replaces out-of-date assemblies with the latest versions. To accomplish this, it needs to read the

相关标签:
4条回答
  • 2020-12-01 18:23

    Use AssemblyName.GetAssemblyName("assembly.dll");, then parse the name. According to MSDN:

    This will only work if the file contains an assembly manifest. This method causes the file to be opened and closed, but the assembly is not added to this domain.

    0 讨论(0)
  • 2020-12-01 18:24

    Depending on the files, one option might be FileVersionInfo - i.e.

    FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(path)
    string ver = fvi.FileVersion;
    

    The problem is that this depends on the code having the [AssemblyFileVersion] attribute, and it matching the [AssemblyVersion] attribute.

    I think I'd look at the AssemblyName options suggested by others first, though.

    0 讨论(0)
  • 2020-12-01 18:29

    Just for the record: Here's how to get the file version in C#.NET Compact Framework. It's basically from OpenNETCF but quite shorter and exctacted so it can by copy'n'pasted. Hope it'll help...

    public static Version GetFileVersionCe(string fileName)
    {
        int handle = 0;
        int length = GetFileVersionInfoSize(fileName, ref handle);
        Version v = null;
        if (length > 0)
        {
            IntPtr buffer = System.Runtime.InteropServices.Marshal.AllocHGlobal(length);
            if (GetFileVersionInfo(fileName, handle, length, buffer))
            {
                IntPtr fixedbuffer = IntPtr.Zero;
                int fixedlen = 0;
                if (VerQueryValue(buffer, "\\", ref fixedbuffer, ref fixedlen))
                {
                    byte[] fixedversioninfo = new byte[fixedlen];
                    System.Runtime.InteropServices.Marshal.Copy(fixedbuffer, fixedversioninfo, 0, fixedlen);
                    v = new Version(
                        BitConverter.ToInt16(fixedversioninfo, 10), 
                        BitConverter.ToInt16(fixedversioninfo,  8), 
                        BitConverter.ToInt16(fixedversioninfo, 14),
                        BitConverter.ToInt16(fixedversioninfo, 12));
                }
            }
            Marshal.FreeHGlobal(buffer);
        }
        return v;
    }
    
    [DllImport("coredll", EntryPoint = "GetFileVersionInfo", SetLastError = true)]
    private static extern bool GetFileVersionInfo(string filename, int handle, int len, IntPtr buffer);
    [DllImport("coredll", EntryPoint = "GetFileVersionInfoSize", SetLastError = true)]
    private static extern int GetFileVersionInfoSize(string filename, ref int handle);
    [DllImport("coredll", EntryPoint = "VerQueryValue", SetLastError = true)]
    private static extern bool VerQueryValue(IntPtr buffer, string subblock, ref IntPtr blockbuffer, ref int len);
    
    0 讨论(0)
  • 2020-12-01 18:47

    I found the following in this article.

    using System.Reflection;
    using System.IO;
    
    ...
    
    // Get current and updated assemblies
    AssemblyName currentAssemblyName = AssemblyName.GetAssemblyName(currentAssemblyPath);
    AssemblyName updatedAssemblyName = AssemblyName.GetAssemblyName(updatedAssemblyPath);
    
    // Compare both versions
    if (updatedAssemblyName.Version.CompareTo(currentAssemblyName.Version) <= 0)
    {
        // There's nothing to update
        return;
    }
    
    // Update older version
    File.Copy(updatedAssemblyPath, currentAssemblyPath, true);
    
    0 讨论(0)
提交回复
热议问题