OVH Cloud OVH Cloud

Comment on récupere la version de l'application sur laquelle on travail ??

5 réponses
Avatar
Sylvain MALLEVAL
le numero de version qui se trouve dans l'assembly

Merci

Sylvain

5 réponses

Avatar
Patrick Philippot
Sylvain MALLEVAL wrote:
le numero de version qui se trouve dans l'assembly



Bonjour,

En C# par exemple:

Assembly.GetAssembly(this.GetType()).GetName().Version

--
Patrick Philippot - Microsoft MVP
MainSoft Consulting Services
www.mainsoft.fr
Avatar
Elp
Sylvain MALLEVAL wrote:
le numero de version qui se trouve dans l'assembly



Version version System.Reflection.Assembly.GetCallingAssembly().GetName().Version;

Apres tu peux utiliser les propriétés de ton object Version pour récupérer
le numéro de version majeur, mineur...
Avatar
Patrick Philippot
Elp wrote:
GetCallingAssembly()



Oui, ça c'est mieux que GetAssembly(this, GetType()), quoique qu'à la la
limite ça doit revenir au même. Il faudrait voir l'implémentation.

--
Patrick Philippot - Microsoft MVP
MainSoft Consulting Services
www.mainsoft.fr
Avatar
Ambassadeur Kosh
je te propose une classe qui te servira peut être...

using System;
using System.Reflection;
using System.Drawing;
using System.IO;


public class AssemblyVersionInfo
{
private Assembly assembly ;

/// <summary>
/// construit un AssemblyVersionInfo à partir de l'assembly en cours
d'execution.
/// </summary>
/// <returns>l'AssemblyVersionInfo</returns>
public static AssemblyVersionInfo GetFromExecutingAssembly()
{
return new AssemblyVersionInfo(Assembly.GetExecutingAssembly()) ;
}

/// <summary>
/// construit un AssemblyVersionInfo fondée sur un Assembly spécifique
/// </summary>
/// <param name="assembly"></param>
public AssemblyVersionInfo(Assembly assembly)
{
this.assembly = assembly ;
}

/// <summary>
/// extrait un attribut spécifique de l'assembly
/// </summary>
/// <param name="type">type d'attribut recherché</param>
/// <returns>l'attribut si 1 seule instance trouvé. null sinon</returns>
private System.Attribute GetAttribute(Type type)
{
object[] v = assembly.GetCustomAttributes(type,false) ;

if(v.Length==1)
{
return v[0] as Attribute ;
}
else
{
return null ;
}
}

/// <summary>
/// version de l'assembly
/// </summary>
public string Version
{
get
{
return assembly.GetName().Version.ToString() ;
}
}

/// <summary>
/// titre de l'assembly
/// </summary>
public string Title
{
get
{
AssemblyTitleAttribute attribute =
GetAttribute(typeof(AssemblyTitleAttribute)) as AssemblyTitleAttribute ;

if(attribute!=null)
{
return attribute.Title ;
}
else
{
return "" ;
}
}
}

/// <summary>
/// LegalCopyright Gets all copyright notices that
/// apply to the specified file.
/// </summary>
/// <param name="assembly"></param>
/// <returns></returns>
public string LegalCopyright
{
get
{
AssemblyCopyrightAttribute attribute =
GetAttribute(typeof(AssemblyCopyrightAttribute)) as
AssemblyCopyrightAttribute ;

if(attribute!=null)
{
return attribute.Copyright ;
}
else
{
return "" ;
}
}
}

/// <summary>
/// LegalTrademarks Gets the trademarks and registered
/// trademarks that apply to the file.
/// </summary>
/// <param name="assembly"></param>
/// <returns></returns>
public string LegalTrademarks
{
get
{
AssemblyTrademarkAttribute attribute =
GetAttribute(typeof(AssemblyTrademarkAttribute)) as
AssemblyTrademarkAttribute ;

if(attribute!=null)
{
return attribute.Trademark ;
}
else
{
return "" ;
}
}
}

/// <summary>
/// recupere l'icone de l'appli
/// </summary>
public Icon AppIcon
{
get
{
// bon, ça marche à moitié.
string [] res = assembly.GetManifestResourceNames() ;
Stream stream = assembly.GetManifestResourceStream("WinOptim.App.ico") ;
Icon icon = new Icon(stream) ;
return icon ;
}
}

// Comments Gets the comments associated with the file.
// CompanyName Gets the name of the company that produced the file.
// FileDescription Gets the description of the file.
// FileMajorPart Gets the major part of the version number.
// FileMinorPart Gets the minor part of the version number of the file.
// FileName Gets the name of the file that this instance of FileVersionInfo
describes.
// FilePrivatePart Gets the file private part number.
// FileVersion Gets the file version number.
// InternalName Gets the internal name of the file, if one exists.
// IsDebug Gets a value that specifies whether the file contains debugging
information or is compiled with debugging features enabled.
// IsPatched Gets a value that specifies whether the file has been modified
and is not identical to the original shipping file of the same version
number.
// IsPreRelease Gets a value that specifies whether the file is a
development version, rather than a commercially released product.
// IsPrivateBuild Gets a value that specifies whether the file was built
using standard release procedures.
// IsSpecialBuild Gets a value that specifies whether the file is a special
build.
// Language Gets the default language string for the version info block.
// OriginalFilename Gets the name the file was created with.
// PrivateBuild Gets information about a private version of the file.
// ProductBuildPart Gets the build number of the product this file is
associated with.
// ProductMajorPart Gets the major part of the version number for the
product this file is associated with.
// ProductMinorPart Gets the minor part of the version number for the
product the file is associated with.
// ProductName Gets the name of the product this file is distributed with.
// ProductPrivatePart Gets the private part number of the product this file
is associated with.
// ProductVersion Gets the version of the product this file is distributed
with.
// SpecialBuild Gets the special build information for the file.
}
Avatar
Zazar
Bonjour,

> GetCallingAssembly()

Oui, ça c'est mieux que GetAssembly(this, GetType()), quoique qu'à la la
limite ça doit revenir au même. Il faudrait voir l'implémentation.



Attention ! GetCallingAssembly renvoie l'assembly qui contient le code qui a
appelé la méthode exécutant la méthode GetCallingAssembly ().
GetExecutingAssembly() renvoie au contraire l'assembly le code qui a appelé
la méthode GetExecutingAssembly().

Exemple :

public class Class1
{
public static System.Reflection.Assembly Get() {
return System.Reflection.Assembly.GetCallingAssembly();
}
public static System.Reflection.Assembly Get2() {
return Get();
}
}

Supposons que cette classe soit dans un Assembly A.
Si on appelle la méthode Get() depuis l'assembly A, elle renverra A.
Si on l'appelle depuis un assembly B, elle renverra B.
Si on appelle Get2() depuis B, ça renvoie A (si pas d'optimisation) ou B (si
optimisations car la méthode Get va se faire inliné)

Bref, c'est assez dangereux d'utiliser GetCallingAssembly(), le résultat
risque d'être différent de celui attendu.

Au contraire, avec GetExecutingAssembly(), on n'a pas de surprise et c'est
cette méthode qui devrait être utilisée dans le cas de l'OP (si j'ai bien
compris ce qu'il voulait).

GetAssembly(this, GetType()) renvoie la même chose que
GetExecutingAssembly() sauf si la classe est héritée depuis un autre
assembly.

--
Zazar