Jak Mogę uzyskać wersję pliku assembly

W {[1] } istnieją dwie wersje montażowe:

  1. AssemblyVersion: określa wersję przypisanego zestawu.
  2. AssemblyFileVersion: poleca kompilatorowi użycie określonego numeru wersji dla zasobu wersji pliku Win32. Wersja pliku Win32 nie musi być taka sama jak numer wersji zestawu.

Mogę uzyskać Assembly Version z następującą linijką kodu:

Version version = Assembly.GetEntryAssembly().GetName().Version;

Ale jak Mogę dostać Assembly File Version?

Author: Rohit, 2009-05-26

6 answers

Zobacz mój komentarz powyżej z prośbą o wyjaśnienie, czego naprawdę chcesz. Mam nadzieję, że to jest to:

System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
string version = fvi.FileVersion;
 729
Author: Xiaofu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-05-12 16:13:23

Istnieją trzy wersje: Montaż, Plik i Produkt. Są one używane przez różne funkcje i przyjmują różne wartości domyślne, jeśli nie określisz ich jawnie.

string assemblyVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString(); 
string assemblyVersion = Assembly.LoadFile('your assembly file').GetName().Version.ToString(); 
string fileVersion = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion; 
string productVersion = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductVersion;
 142
Author: Check6,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-19 17:15:17

Gdy chcę uzyskać dostęp do wersji pliku aplikacji (co jest ustawione w Assembly Information - > File version), powiedzmy, aby ustawić tekst etykiety na nim przy ładowaniu formularza, aby wyświetlić wersję, użyłem właśnie

versionlabel.Text = "Version " + Application.ProductVersion;
 56
Author: syntap,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-14 11:43:06

Aktualizacja: jak wspomniał Richard Grimes w mój cytowany post, @Iain i @ Dmitry Lobanov, moja odpowiedź jest dobra w teorii, ale zła w praktyce.

Jak powinienem był pamiętać z niezliczonych książek, itp., podczas gdy ustawia się te właściwości za pomocą [assembly: XXXAttribute], są one highjackowane przez kompilator i umieszczane w zasobie VERSIONINFO.

Z powyższego powodu, musisz użyć podejścia w @ xiaofu ' s answer ponieważ atrybuty są usuwane po tym, jak sygnał został wydobyte z nich.


public static string GetProductVersion()
{
  var attribute = (AssemblyVersionAttribute)Assembly
    .GetExecutingAssembly()
    .GetCustomAttributes( typeof(AssemblyVersionAttribute), true )
    .Single();
   return attribute.InformationalVersion;
}

(od http://bytes.com/groups/net/420417-assemblyversionattribute - Jak tam zaznaczono, jeśli szukasz innego atrybutu, zastąp go powyższym)

 20
Author: Ruben Bartelink,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:26:36

Użyj tego:

((AssemblyFileVersionAttribute)Attribute.GetCustomAttribute(
    Assembly.GetExecutingAssembly(), 
    typeof(AssemblyFileVersionAttribute), false)
).Version;

Lub to:

new Version(System.Windows.Forms.Application.ProductVersion);
 3
Author: Pieter Geerkens,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-30 14:33:26

Możesz uzyskać wersję montażową za pomocą My.Application.Info.Version

 -4
Author: Viacheslav,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-09-11 15:41:10