您的位置:首页 > 其它

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也可用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: