我如何得到一个程序集的版本,而无需加载呢? [英] How do I get the version of an assembly without loading it?

查看:144
本文介绍了我如何得到一个程序集的版本,而无需加载呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个大计划的一个小功能检查文件夹中的组件,并取代了过期组件的最新版本。要做到这一点,它需要读取现有的汇编文件的版本号,而无需实际装载这些组件到执行的进程。

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.

推荐答案

我发现下面的<一个href="http://blogs.msdn.com/alejacma/archive/2008/09/05/how-to-get-assembly-version-without-loading-it.aspx">in这篇文章。

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

这篇关于我如何得到一个程序集的版本,而无需加载呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆