C#中获取版本号的方法

转载自:http://www.cnblogs.com/gavincome/archive/2007/12/15/995665.html

 

1、
public   void   GetFileVersion()   {  
          //   Get   the   file   version   for   the   notepad.  
          FileVersionInfo   myFileVersionInfo   =   FileVersionInfo.GetVersionInfo("%systemroot%//Notepad.exe");  
     
          //   Print   the   file   name   and   version   number.  
          textBox1.Text   =   "File:   "   +   myFileVersionInfo.FileDescription   +   '/n'   +  
                "Version   number:   "   +   myFileVersionInfo.FileVersion;  
    }  

2、
 //   有关程序集的常规信息是通过下列  
  //   属性集控制的。更改这些属性值可修改与程序集  
  //   关联的信息。  
  //  
  [assembly:   AssemblyTitle("")]  
  [assembly:   AssemblyDescription("")]  
  [assembly:   AssemblyConfiguration("")]  
  [assembly:   AssemblyCompany("")]  
  [assembly:   AssemblyProduct("")]  
  [assembly:   AssemblyCopyright("")]  
  [assembly:   AssemblyTrademark("")]  
  [assembly:   AssemblyCulture("")]  
   
  修改AssemblyInfo.cs文件的上述信息  
   
  在程序中调用如下:  
  Version   ApplicationVersion   =   new   Version(Application.ProductVersion);  
  string   ss   =   ApplicationVersion.Major;//获取主版本号  

3、
我认为比较好的做法  
  private   void   GetEdition()  
  {  
  Assembly   assembly   =   Assembly.GetExecutingAssembly();  
  //this.labelEdition.Text   =   assembly.FullName;  
   
  //   获取程序集元数据  
  AssemblyCopyrightAttribute   copyright   =   (AssemblyCopyrightAttribute)    
  AssemblyCopyrightAttribute.GetCustomAttribute(Assembly.GetExecutingAssembly(),  
  typeof(AssemblyCopyrightAttribute));  
  AssemblyDescriptionAttribute   description   =   (AssemblyDescriptionAttribute)    
  AssemblyDescriptionAttribute.GetCustomAttribute(System.Reflection.Assembly.GetExecutingAssembly(),  
  typeof(AssemblyDescriptionAttribute));  
   
  this.labelAppName.Text   =   description.Description;  
  //this.labelEdition.Text   =   description.Description;  
  //this.labelEdition.Text   =   copyright.Copyright;  
  this.labelEdition.Text   =   Application.ProductVersion;  
  }


4、
使用反射  
   
              string   path   =   @"C:/WINNT/Microsoft.NET/Framework/v1.1.4322/System.dll";  
              Assembly   assembly   =   Assembly.LoadFile(path);  
              AssemblyName   assemblyName   =   assembly.GetName();  
              Version   version   =   assemblyName.Version;  
              Console.WriteLine(assemblyName.FullName);  
              Console.WriteLine("Major   Version         =   "   +   version.Major);  
              Console.WriteLine("Minor   Version         =   "   +   version.Minor);  
              Console.WriteLine("Revision   Version   =   "   +   version.Revision);  
              Console.WriteLine("Build   Version         =   "   +   version.Build);  
   
  只要得到Assembly   ,剩下的都好说,  
  楼主可以看看Assembly的几个静态方法,应该可以满足所有需要了  
  比如  
  //得到Mscorlib.dll  
  Assembly.GetAssembly(typeof(string))   

 

------------------------------------------------------------------------

 

备忘,第3种方法最通用,.NETCF35也可用

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值