C#中获取版本号的方法
2010-10-20 14:35
260 查看
转载自: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、
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也可用
相关文章推荐
- C#中获取版本号的方法
- C#中获取版本号的方法
- C#中获取版本号的方法
- C#中获取程序集版本号的方法
- C#中获取版本号的方法
- C#中获取程序集版本号的方法
- python中获取python版本号的方法
- 获取浏览器版本号的方法
- jQuery获取浏览器类型和版本号的方法
- 获取iOS项目名称及版本号以及网络类型的方法
- WPF获取程序版本号(Version)的方法
- c++获取OpenGL版本号的两种方法
- C#中获取版本号的方法
- JS获取IE版本号与HTML设置IE文档模式的方法
- .net获取版本号的三种方法
- wp7 获取应用信息、版本号的方法
- 安卓中获取应用程序版本号和版本名称的方法
- 获取DLL文件版本号的三种方法
- Android 获取未安装的APK图标、版本号、包名等信息方法
- jquery获取浏览器类型和版本号的方法