如何在不加载程序集的情况下获取程序集的版本?

时间:2023-01-14 14:33:23

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 version numbers of the existing assembly files without actually loading those assemblies into the executing process.

大型程序的一个小功能检查文件夹中的程序集,并用最新版本替换过时的程序集。要实现此目的,它需要读取现有程序集文件的版本号,而不实际将这些程序集加载到执行过程中。

4 个解决方案

#1


33  

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);

#2


11  

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

根据文件,一个选项可能是FileVersionInfo - 即

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.

问题是这取决于具有[AssemblyFileVersion]属性的代码,并且它与[AssemblyVersion]属性匹配。

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

我想我先看看其他人建议的AssemblyName选项。

#3


9  

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

使用AssemblyName.GetAssemblyName(“assembly.dll”);,然后解析名称。根据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.

这仅在文件包含程序集清单时才有效。此方法导致文件打开和关闭,但程序集不会添加到此域。

#4


1  

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...

仅供记录:以下是如何在C#.NET Compact Framework中获取文件版本。它基本上来自OpenNETCF,但相当短,并且可以通过copy'n'pasted来实现。希望它会有所帮助......

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);

#1


33  

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);

#2


11  

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

根据文件,一个选项可能是FileVersionInfo - 即

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.

问题是这取决于具有[AssemblyFileVersion]属性的代码,并且它与[AssemblyVersion]属性匹配。

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

我想我先看看其他人建议的AssemblyName选项。

#3


9  

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

使用AssemblyName.GetAssemblyName(“assembly.dll”);,然后解析名称。根据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.

这仅在文件包含程序集清单时才有效。此方法导致文件打开和关闭,但程序集不会添加到此域。

#4


1  

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...

仅供记录:以下是如何在C#.NET Compact Framework中获取文件版本。它基本上来自OpenNETCF,但相当短,并且可以通过copy'n'pasted来实现。希望它会有所帮助......

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);