Windows Store 应用中获取程序集版本号的方法

本文为个人博客备份文章,原文地址:

http://validvoid.net/windows-store-app-get-assembly-version/

WinRT 中对反射做了很多限制,假设 Windows Store 应用引用了一个程序集 MyApp.Utils,其中有一个类叫做 MyUtils,可以使用以下方法获得程序集 MyApp.Utils的版本号和文件版本号。

获得程序集版本号

方法一

using System.Reflection;

public static string GetAssemblyVersion()
{
    return Assembly.Load(new AssemblyName("MyApp.Utils"))
        .GetName().Version.ToString();
}

注意方法一有两个限制条件,一是要求知道程序集的完整显示名称;二是程序集输出类型必须是“类库”(Class Library)。当程序集输出类型是“Windows 运行时组件”(Windows Runtime Component)时,方法一将返回如下错误:

Could not load file or assembly ‘MyApp.Utils, Culture=neutral, PublicKeyToken=null‘ or one of its dependencies. The system cannot find the file specified.

方法二

using System.Reflection;

public static string GetAssemblyVersion()
{
    return typeof(MyUtils).GetTypeInfo().Assembly.GetName().Version.ToString();
}

方法二在程序集输出类型为类库或者 Windows 运行时组件时均可用,其限制条件是需知道程序集中的一个类,再通过 typeof(MyUtils).GetTypeInfo().Assembly 取得目标程序集,进而获得程序集的版本号。

获得程序集文件版本号

using System.Reflection;

public static string GetAssemblyFileVersion()
{
    return
        CustomAttributeExtensions.GetCustomAttribute(
            typeof(MyUtils).GetTypeInfo().Assembly).Version;
}

CustomAttributeExtensions 类包含了可以获得自定义特性的静态方法,可以通过此类,获得 AssemblyFileVersionAttribute 特性的值,也就是程序集的文件版本号。

可用性

以上方法在 Windows/ Windows Phone 8.1 Universal 以及 Windows 10 UWP 中测试可用。

参考

时间: 2024-08-24 03:21:06

Windows Store 应用中获取程序集版本号的方法的相关文章

【转】python中获取python版本号的方法

原文 python3 #!/usr/bin/python # 第1种方法 import platform print(platform.python_version()) # 第2种方法 import sys print(sys.version) print(sys.version_info) >>> import sys >>> sys.version '3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 18:11:49) [MSC v.1900

python中获取python版本号的方法【转】

1 2 3 4 #!/usr/bin/python  # 第1种方法 import platform  print(platform.python_version()) 1 2 3 >>> import platform >>> print(platform.python_version()) 3.6.3 1 2 3 4 # 第2种方法 import sys  print(sys.version) print(sys.version_info) 来自:https://b

Windows Store App 中使用Scrollviewer实现缩放功能

Windows Store App中可以直接使用Scrollviewer实现缩放功能,基本代码如下: <ScrollViewer Background="Yellow" HorizontalScrollBarVisibility="Visible" MaxZoomFactor="2" MinZoomFactor="0.5" VerticalScrollBarVisibility="Visible" Z

Windows Store App 中挂起时,程序奔溃的问题

昨天在开发windows phone 8.1程序时,发现程序在挂起的时候,会自动退出.通过调试发现错误信息是 System.Runtime.InteropServices.COMException (0x80004005): Unspecified error GetNavigationState doesn't support serialization of a parameter type which was passed to Frame.Navigate. at Windows.UI.

android中获取root权限的方法以及原理(转)

一. 概述 本文介绍了android中获取root权限的方法以及原理,让大家对android 玩家中常说的“越狱”有一个更深层次的认识. 二. Root 的介绍 1. Root 的目的 可以让我们拥有掌控手机系统的权限,比如删除一些system/app下面的无用软件,更换开关机铃声和动画,拦截状态栏弹出的广告等. 2. Root的原理介绍 谷歌的android系统管理员用户就叫做root,该帐户拥有整个系统至高无上的权利,它可以访问和修改你手机几乎所有的文件,只有root才具备最高级别的管理权限

如何在Windows、Linux中获取主机的网络信息和公网地址

在我们的日常工作中,我们会需要进行查询主机当下的网络信息和公网上看到的IP地址信息,他们就像是网路上的名片,让我们找到彼此. Windows下如何获取内网和公网信息 在windows下该如何获取网路信息呢? 打开windows下的命令行,输入如下命令:  大家可以看到,在上述的截图中列出了内网地址,DNS信息,网关信息,内网地址,外网地址等等信息,包括无线还是优先的网卡等等信息. 这里截取了某个无线网卡的网络信息,做一个简单的示例. 那如何获取其公网信息呢? -** baidu.com中输入公网

Python的raw_input语句包含中文,在Windows环境CMD中显示乱码的解决方法

Python的raw_input语句中如果包含中文,在Windows环境CMD中执行时会显示乱码: 由于raw_input语句不支持unicode显示,在中文字符串前加u会报错: 解决办法: 在语句中进行强制的编码转换,格式如下: shuzi = raw_input (unicode('请输入日期:','utf-8').encode('gbk'))

asp.net中获取当前url的方法

HttpContext.Current.Request.Url.ToString() 并不可靠. 如果当前URL为 http://localhost/search.aspx?user=http://csharp.xdowns.com&tag=%BC%BC%CA%F5 通过HttpContext.Current.Request.Url.ToString()获取到的却是 http://localhost/search.aspxuser=http://csharp.xdowns.com&tag=

【转】Java中获取文件大小的正确方法

本文出处:http://blog.csdn.net/chaijunkun/article/details/22387305,转载请注明.由于本人不定期会整理相关博文,会对相应内容作出完善.因此强烈建议在原始出处查看此文. 今天写代码时需要实现获取文件大小的功能,目前有两种实现方法,一种是使用File的length()方法:另外一种是使用 FileInputStream的available()方法,当InputStream未进行read操作时,available()的大小应该是等于 文件大小的.但