2009-03-31 32 views
32

tôi đang làm việc trên một chương trình, và tôi đang cố gắng để hiển thị lắp ráp FILE phiên bảnC sử dụng # AssemblyFileVersion trong một chương trình

public static string Version 
    { 
     get 
     { 
      Assembly asm = Assembly.GetExecutingAssembly(); 
      FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location); 
      return String.Format("{0}.{1}", fvi.FileMajorPart, fvi.FileMinorPart); 
     } 
    } 

Tại thời điểm này, điều này chỉ trả về hai số phiên bản đầu tiên trong "AssemblyVersion", không phải "AssemblyFileVersion". Tôi thực sự muốn chỉ cần tham khảo các AssemblyFileVersion chứ không phải lưu trữ một biến nội bộ gọi là "phiên bản" mà tôi phải cập nhật cả này và phiên bản lắp ráp ...

[assembly: AssemblyVersion("1.0.*")] 
[assembly: AssemblyFileVersion("3.5.0")] 

Đó là AssemblyFileVersion tôi từ AssemblyInfo.cs. Tôi muốn chỉ cần tham khảo các "3.5.x" một phần, không phải là "1.0 *.":/

Cảm ơn, Zack

+1

Bạn đã là 99% so với cách đó, chỉ cần thay đổi trở lại fvi.FileVersion – McAden

+0

thấy https://stackoverflow.com/a/12528418/492 để có được những thông tin phiên bản từ một cụ thể DLL, chứ không phải là ứng dụng thực thi cha mẹ. –

Trả lời

33

Sử dụng ProductMajorPart/ProductMinorPart thay vì FileMajorPart/FileMinorPart:

public static string Version 
    { 
     get 
     { 
      Assembly asm = Assembly.GetExecutingAssembly(); 
      FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location); 
      return String.Format("{0}.{1}", fvi.ProductMajorPart, fvi.ProductMinorPart); 
     } 
    } 
+0

Err. Tôi chỉ nhận ra nó nhận được phiên bản của dll! Không chính xác những gì tôi muốn. Tôi muốn phiên bản thực thi. (Quên đề cập rằng nó đang được gọi từ một .dll) – Zack

+4

Assembly asm = Assembly.GetEntryAssembly(); – Diadistis

+5

Lưu ý rằng fvi.FileVersion sẽ cung cấp cho bạn chuỗi định dạng trực tiếp (tất cả bốn phần), nếu đó là những gì bạn cần ... – Benjol

1

Tôi đoán bạn sẽ phải sử dụng lớp FileVersionInfo.

System.Diagnostics.FileVersionInfo.GetVersionInfo(FullpathToAssembly)

+0

Ví dụ tôi đã trình bày sử dụng FileVersionInfo. Nhưng nó chỉ trả về thuộc tính "AssemblyVersion". – Zack

+0

Rất tiếc. Đó là ngu ngốc của tôi không phải để xem câu hỏi hoàn toàn. Xin lỗi vì điều đó. – shahkalpesh

3

Để có được phiên bản của hiện đang thực hiện lắp ráp, bạn có thể sử dụng:

using System.Reflection; 
Version version = Assembly.GetExecutingAssembly().GetName().Version; 

Lớp hội có thể cũng tải các tập tin và truy cập tất cả các assembly được nạp trong một tiến trình.

+0

nó là AssemblyVersion, không AssemblyFileVersion – Slava

6
using System.Reflection; 
using System.IO; 

FileVersionInfo fv = System.Diagnostics.FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location); 

Console.WriteLine("AssemblyVersion : {0}", Assembly.GetExecutingAssembly().GetName().Version.ToString()); 

Console.WriteLine ("AssemblyFileVersion : {0}" , fv.FileVersion.ToString()); 
3
var fileVersion = GetCustomAttributeValue<AssemblyFileVersionAttribute>(assembly, "Version"); 

    private static string GetCustomAttributeValue<T>(Assembly assembly, string propertyName) 
     where T : Attribute 
    { 
     if (assembly == null || string.IsNullOrEmpty(propertyName)) return string.Empty; 

     object[] attributes = assembly.GetCustomAttributes(typeof(T), false);    
     if (attributes.Length == 0) return string.Empty; 

     var attribute = attributes[0] as T; 
     if (attribute == null) return string.Empty; 

     var propertyInfo = attribute.GetType().GetProperty(propertyName); 
     if (propertyInfo == null) return string.Empty; 

     var value = propertyInfo.GetValue(attribute, null); 
     return value.ToString(); 
    } 
+0

Điều này thực sự hoạt động! – Roboblob

Các vấn đề liên quan