求助,SL获取程序AssemblyInfo.cs文件中版本信息失败

fallinforyou 2011-11-23 02:19:03
我用如下方法获取程序的版本信息
string version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();

会报如下的错误
访问安全关键方法“System.Reflection.Assembly.GetName()”的尝试失败。

请大师指教?
...全文
50 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
fallinforyou 2011-11-23
  • 打赏
  • 举报
回复
谢谢版主的热心帮助,您的两种方法都可以实现.让我受益非浅.
总结一下,获取AssemblyInfo.cs中的版本有以下两种方法

方法一:
AssemblyName assemblyName = new AssemblyName(System.Reflection.Assembly.GetExecutingAssembly().FullName) ;
string version1 = assemblyName.Version.ToString();

方法二:
String version2 = System.Reflection.Assembly.GetExecutingAssembly().FullName.Split(',')[1].Split('=')[1];
jv9 2011-11-23
  • 打赏
  • 举报
回复
使用fullname属性替换getname方法。

可以考虑使用:

private static Version ParseVersionNumber(Assembly assembly)
{
AssemblyName assemblyName = new AssemblyName(assembly.FullName);
return assemblyName.Version;
}


或者:


Assembly assembly = Assembly.GetExecutingAssembly();
String version = assembly.FullName.Split(',')[1];
String fullversion = version.Split('=')[1];
jv9 2011-11-23
  • 打赏
  • 举报
回复
试着使用这个代码:

Assembly SampleAssembly;
// Instantiate a target object.
Int32 Integer1 = new Int32();
Type Type1;
// Set the Type instance to the target class type.
Type1 = Integer1.GetType();
// Instantiate an Assembly class to the assembly housing the Integer type.
SampleAssembly = Assembly.GetAssembly(Integer1.GetType());
// Display the name of the assembly currently executing
Console.WriteLine("GetExecutingAssembly=" + Assembly.GetExecutingAssembly().FullName);

8,734

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧