C#中获取版本号的方法

发布时间:2009年06月29日      浏览次数:826 次
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))
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!