Accueil > > > OBTENIR LE NOM DETAILLE DU SYSTEME D'EXPLOITATION
OBTENIR LE NOM DETAILLE DU SYSTEME D'EXPLOITATION
Information sur la source
Description
Ce que je reproche à System.OperatingSystem (même pour la beta 2.0) c'est le nom du système qu'on obtient est un nom du genre "Microsoft Windows <Version>" ou "Microsoft Windows NT <version>" au lieu du nom du produit du genre "Windows 98" ou "Windows XP". J'ai donc essayé de faire une classe qui donne des informations détaillées sur la version du système utilisé comme le nom du produit, l'édition utilisée et le service pack installé (si c'est le cas). J'ai donc regroupé la plupart des versions de Windows faisant tourner .NET (Windows 98/ME, NT4/2000/XP/2003) mais pas les versions mobiles (CE, XP Embedded, ...) moins utilisées.
Source
- using System;
- using System.Text;
- using System.Runtime.InteropServices;
-
- namespace Xya
- {
- /// <summary>
- /// Représente le nom et la version d'un système d'exploitation exécutant le Runtime .NET.
- /// </summary>
- public class OSVersion
- {
- #region Champs
- static OSVersion current;
- OSVERSIONINFOEX osVersion;
- PlatformID platform;
- string fullName;
- string name;
- string edition;
- string sp;
- Version version;
- Version spVersion;
- #endregion
- #region Propriétés
- /// <summary>
- /// Obtient la version du système d'exploitation utilisé.
- /// </summary>
- public static OSVersion Current
- {
- get
- {
- lock( typeof(OSVersion) )
- {
- if( current == null )
- {
- current = new OSVersion();
- }
- return current;
- }
- }
- }
- /// <summary>
- /// Obtient le nom complet du système d'exploitation utilisé, avec le nom, l'édition et le service pack installé si applicable.
- /// </summary>
- public string FullName
- {
- get
- {
- if( fullName == null )
- {
- fullName = ToString();
- }
- return fullName;
- }
- }
- /// <summary>
- /// Obtient le nom du système d'exploitation utilisé.
- /// </summary>
- public string ProductName
- {
- get
- {
- return name;
- }
- }
- /// <summary>
- /// Obtient l'édition du système d'exploitation utilisé, si applicable.
- /// </summary>
- public string Edition
- {
- get
- {
- return edition;
- }
- }
- /// <summary>
- /// Obtient la plate-forme du système d'exploitation utilisé.
- /// </summary>
- public PlatformID Platform
- {
- get
- {
- return platform;
- }
- }
- /// <summary>
- /// Obtient le nom du service pack installé sur le système, si applicable.
- /// </summary>
- public string ServicePack
- {
- get
- {
- return sp;
- }
- }
- /// <summary>
- /// Obtient la version du système d'exploitation utilisé.
- /// </summary>
- public Version Version
- {
- get
- {
- return version;
- }
- }
- /// <summary>
- /// Obtient la version du service pack installé sur le système, si applicable.
- /// </summary>
- public Version ServicePackVersion
- {
- get
- {
- return spVersion;
- }
- }
- #endregion
- #region Constructeur
- private OSVersion()
- {
- osVersion = new OSVERSIONINFOEX();
- osVersion.dwOSVersionInfoSize = Marshal.SizeOf( osVersion );
- GetVersionEx( ref osVersion );
- platform = (PlatformID)osVersion.dwPlatformId;
- version = new Version( osVersion.dwMajorVersion,
- osVersion.dwMinorVersion,
- osVersion.dwBuildNumber,
- 0 );
- name = GetWindowsName();
- edition = GetWindowsEdition();
- if( osVersion.wServicePackMajor > 0 )
- {
- sp = osVersion.szCSDVersion;
- spVersion = new Version( osVersion.wServicePackMajor,
- osVersion.wServicePackMinor,
- 0,
- 0 );
- }
- else
- {
- sp = null;
- spVersion = null;
- }
- }
- #endregion
- #region Appels P/Invoke
- private struct OSVERSIONINFOEX
- {
- public int dwOSVersionInfoSize;
- public int dwMajorVersion;
- public int dwMinorVersion;
- public int dwBuildNumber;
- public int dwPlatformId;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)]
- public string szCSDVersion;
- public short wServicePackMajor;
- public short wServicePackMinor;
- public short wSuiteMask;
- public byte wProductType;
- public byte wReserved;
- }
-
- [DllImport("kernel32.dll")]
- private static extern int GetVersionEx( [MarshalAs(UnmanagedType.Struct)] ref OSVERSIONINFOEX osinfo );
- private const int VER_NT_WORKSTATION = 0x00000001;
- private const int VER_SUITE_ENTERPRISE = 0x00000002;
- private const int VER_SUITE_BACKOFFICE = 0x00000004;
- private const int VER_SUITE_COMMUNICATIONS = 0x00000008;
- private const int VER_SUITE_TERMINAL = 0x00000010;
- private const int VER_SUITE_SMALLBUSINESS_RESTRICTED = 0x00000020;
- private const int VER_SUITE_EMBEDDEDNT = 0x00000040;
- private const int VER_SUITE_DATACENTER = 0x00000080;
- private const int VER_SUITE_SINGLEUSERTS = 0x00000100;
- private const int VER_SUITE_PERSONAL = 0x00000200;
- private const int VER_SUITE_BLADE = 0x00000400;
- private const int VER_SUITE_EMBEDDED_RESTRICTED = 0x00000800;
- #endregion
- #region Implémentation
- private string GetWindowsName()
- {
- switch( (PlatformID)osVersion.dwPlatformId )
- {
- case PlatformID.Win32Windows:
- {
- //4.1.1998 Windows 98
- //4.1.2222 Windows 98 SE
- //4.9.3000 Windows ME
- switch( osVersion.dwMinorVersion )
- {
- case 1:
- {
- switch( osVersion.dwBuildNumber )
- {
- case 1998:
- return "Windows 98";
- case 2222:
- return "Windows 98 SE";
- default:
- return "Windows 98";
- }
- }
- case 9:
- {
- return "Windows ME";
- }
- default:
- {
- return "Windows " + version.ToString();
- }
- }
- }
- case PlatformID.Win32NT:
- {
- //4.0.1381 Windows NT 4.0
- //5.0.2195 Windows 2000
- //5.1.2600 Windows XP
- //5.2.3790 Windows Server 2003
- switch( osVersion.dwMajorVersion )
- {
- case 4:
- {
- return "Windows NT 4.0";
- }
- case 5:
- {
- switch( osVersion.dwMinorVersion )
- {
- case 0:
- {
- return "Windows 2000";
- }
- case 1:
- {
- return "Windows XP";
- }
- case 2:
- {
- return "Windows Server 2003";
- }
- default:
- {
- return "Windows NT " + version.ToString();
- }
- }
- }
- default:
- {
- return "Windows NT " + version.ToString();
- }
- }
- }
- case PlatformID.WinCE:
- {
- return "Windows CE " + version.ToString();
- }
- default:
- {
- return "Windows " + version.ToString();
- }
- }
- }
- private string GetWindowsEdition()
- {
- string version = this.version.ToString();
-
- if( (osVersion.wSuiteMask & VER_SUITE_BLADE) == VER_SUITE_BLADE &&
- version.StartsWith( "5.2" ) )
- {
- //Windows Server 2003 Web Edition
- return "Web Edition";
- }
- else if( (osVersion.wSuiteMask & VER_SUITE_DATACENTER) == VER_SUITE_DATACENTER )
- {
- if( version.StartsWith( "5.0" ) )
- {
- //Windows 2000 Datacenter Server
- return "Datacenter Server";
- }
- else if( version.StartsWith( "5.2" ) )
- {
- //Windows 2003 Datacenter Edition
- return "Datacenter Edition";
- }
- else
- {
- return "Datacenter Edition";
- }
- }
- else if( (osVersion.wSuiteMask & VER_SUITE_ENTERPRISE) == VER_SUITE_ENTERPRISE )
- {
- if( version.StartsWith( "4.0" ) )
- {
- //Windows NT 4.0 Enterprise Edition
- return "Enterprise Edition";
- }
- else if( version.StartsWith( "5.0" ) )
- {
- //Windows 2000 Advanced Server
- return "Advanced Server";
- }
- else if( version.StartsWith( "5.2" ) )
- {
- //Windows 2003 Server Enterprise Edition
- return "Enterprise Edition";
- }
- else
- {
- return "Enterprise Edition";
- }
- }
- else if( osVersion.wProductType == VER_NT_WORKSTATION )
- {
- if( version.StartsWith( "4.0" ) )
- {
- //Windows NT 4.0 Workstation
- return "Workstation";
- }
- else if( version.StartsWith( "5.0" ) )
- {
- //Windows 2000 Professional
- return "Professional";
- }
- else if( version.StartsWith( "5.1" ) )
- {
- if( (osVersion.wSuiteMask & VER_SUITE_PERSONAL) == VER_SUITE_PERSONAL )
- {
- //Windows XP Home Edition
- return "Home Edition";
- }
- else
- {
- //Windows XP Professional
- return "Professional";
- }
- }
- else
- {
- return "Workstation";
- }
- }
- else
- {
- return null;
- }
- }
- /// <summary>
- /// Obtient le nom complet du système d'exploitation utilisé.
- /// </summary>
- /// <returns> Nom complet du système d'exploitation utilisé. </returns>
- public override string ToString()
- {
- StringBuilder sb = new StringBuilder();
-
- sb.Append( name );
- if( edition != null )
- {
- sb.Append( ' ' );
- sb.Append( edition );
- }
- if( sp != null )
- {
- sb.Append( ' ' );
- sb.Append( sp );
- }
- return sb.ToString();
- }
- #endregion
- }
- }
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace Xya
{
/// <summary>
/// Représente le nom et la version d'un système d'exploitation exécutant le Runtime .NET.
/// </summary>
public class OSVersion
{
#region Champs
static OSVersion current;
OSVERSIONINFOEX osVersion;
PlatformID platform;
string fullName;
string name;
string edition;
string sp;
Version version;
Version spVersion;
#endregion
#region Propriétés
/// <summary>
/// Obtient la version du système d'exploitation utilisé.
/// </summary>
public static OSVersion Current
{
get
{
lock( typeof(OSVersion) )
{
if( current == null )
{
current = new OSVersion();
}
return current;
}
}
}
/// <summary>
/// Obtient le nom complet du système d'exploitation utilisé, avec le nom, l'édition et le service pack installé si applicable.
/// </summary>
public string FullName
{
get
{
if( fullName == null )
{
fullName = ToString();
}
return fullName;
}
}
/// <summary>
/// Obtient le nom du système d'exploitation utilisé.
/// </summary>
public string ProductName
{
get
{
return name;
}
}
/// <summary>
/// Obtient l'édition du système d'exploitation utilisé, si applicable.
/// </summary>
public string Edition
{
get
{
return edition;
}
}
/// <summary>
/// Obtient la plate-forme du système d'exploitation utilisé.
/// </summary>
public PlatformID Platform
{
get
{
return platform;
}
}
/// <summary>
/// Obtient le nom du service pack installé sur le système, si applicable.
/// </summary>
public string ServicePack
{
get
{
return sp;
}
}
/// <summary>
/// Obtient la version du système d'exploitation utilisé.
/// </summary>
public Version Version
{
get
{
return version;
}
}
/// <summary>
/// Obtient la version du service pack installé sur le système, si applicable.
/// </summary>
public Version ServicePackVersion
{
get
{
return spVersion;
}
}
#endregion
#region Constructeur
private OSVersion()
{
osVersion = new OSVERSIONINFOEX();
osVersion.dwOSVersionInfoSize = Marshal.SizeOf( osVersion );
GetVersionEx( ref osVersion );
platform = (PlatformID)osVersion.dwPlatformId;
version = new Version( osVersion.dwMajorVersion,
osVersion.dwMinorVersion,
osVersion.dwBuildNumber,
0 );
name = GetWindowsName();
edition = GetWindowsEdition();
if( osVersion.wServicePackMajor > 0 )
{
sp = osVersion.szCSDVersion;
spVersion = new Version( osVersion.wServicePackMajor,
osVersion.wServicePackMinor,
0,
0 );
}
else
{
sp = null;
spVersion = null;
}
}
#endregion
#region Appels P/Invoke
private struct OSVERSIONINFOEX
{
public int dwOSVersionInfoSize;
public int dwMajorVersion;
public int dwMinorVersion;
public int dwBuildNumber;
public int dwPlatformId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)]
public string szCSDVersion;
public short wServicePackMajor;
public short wServicePackMinor;
public short wSuiteMask;
public byte wProductType;
public byte wReserved;
}
[DllImport("kernel32.dll")]
private static extern int GetVersionEx( [MarshalAs(UnmanagedType.Struct)] ref OSVERSIONINFOEX osinfo );
private const int VER_NT_WORKSTATION = 0x00000001;
private const int VER_SUITE_ENTERPRISE = 0x00000002;
private const int VER_SUITE_BACKOFFICE = 0x00000004;
private const int VER_SUITE_COMMUNICATIONS = 0x00000008;
private const int VER_SUITE_TERMINAL = 0x00000010;
private const int VER_SUITE_SMALLBUSINESS_RESTRICTED = 0x00000020;
private const int VER_SUITE_EMBEDDEDNT = 0x00000040;
private const int VER_SUITE_DATACENTER = 0x00000080;
private const int VER_SUITE_SINGLEUSERTS = 0x00000100;
private const int VER_SUITE_PERSONAL = 0x00000200;
private const int VER_SUITE_BLADE = 0x00000400;
private const int VER_SUITE_EMBEDDED_RESTRICTED = 0x00000800;
#endregion
#region Implémentation
private string GetWindowsName()
{
switch( (PlatformID)osVersion.dwPlatformId )
{
case PlatformID.Win32Windows:
{
//4.1.1998 Windows 98
//4.1.2222 Windows 98 SE
//4.9.3000 Windows ME
switch( osVersion.dwMinorVersion )
{
case 1:
{
switch( osVersion.dwBuildNumber )
{
case 1998:
return "Windows 98";
case 2222:
return "Windows 98 SE";
default:
return "Windows 98";
}
}
case 9:
{
return "Windows ME";
}
default:
{
return "Windows " + version.ToString();
}
}
}
case PlatformID.Win32NT:
{
//4.0.1381 Windows NT 4.0
//5.0.2195 Windows 2000
//5.1.2600 Windows XP
//5.2.3790 Windows Server 2003
switch( osVersion.dwMajorVersion )
{
case 4:
{
return "Windows NT 4.0";
}
case 5:
{
switch( osVersion.dwMinorVersion )
{
case 0:
{
return "Windows 2000";
}
case 1:
{
return "Windows XP";
}
case 2:
{
return "Windows Server 2003";
}
default:
{
return "Windows NT " + version.ToString();
}
}
}
default:
{
return "Windows NT " + version.ToString();
}
}
}
case PlatformID.WinCE:
{
return "Windows CE " + version.ToString();
}
default:
{
return "Windows " + version.ToString();
}
}
}
private string GetWindowsEdition()
{
string version = this.version.ToString();
if( (osVersion.wSuiteMask & VER_SUITE_BLADE) == VER_SUITE_BLADE &&
version.StartsWith( "5.2" ) )
{
//Windows Server 2003 Web Edition
return "Web Edition";
}
else if( (osVersion.wSuiteMask & VER_SUITE_DATACENTER) == VER_SUITE_DATACENTER )
{
if( version.StartsWith( "5.0" ) )
{
//Windows 2000 Datacenter Server
return "Datacenter Server";
}
else if( version.StartsWith( "5.2" ) )
{
//Windows 2003 Datacenter Edition
return "Datacenter Edition";
}
else
{
return "Datacenter Edition";
}
}
else if( (osVersion.wSuiteMask & VER_SUITE_ENTERPRISE) == VER_SUITE_ENTERPRISE )
{
if( version.StartsWith( "4.0" ) )
{
//Windows NT 4.0 Enterprise Edition
return "Enterprise Edition";
}
else if( version.StartsWith( "5.0" ) )
{
//Windows 2000 Advanced Server
return "Advanced Server";
}
else if( version.StartsWith( "5.2" ) )
{
//Windows 2003 Server Enterprise Edition
return "Enterprise Edition";
}
else
{
return "Enterprise Edition";
}
}
else if( osVersion.wProductType == VER_NT_WORKSTATION )
{
if( version.StartsWith( "4.0" ) )
{
//Windows NT 4.0 Workstation
return "Workstation";
}
else if( version.StartsWith( "5.0" ) )
{
//Windows 2000 Professional
return "Professional";
}
else if( version.StartsWith( "5.1" ) )
{
if( (osVersion.wSuiteMask & VER_SUITE_PERSONAL) == VER_SUITE_PERSONAL )
{
//Windows XP Home Edition
return "Home Edition";
}
else
{
//Windows XP Professional
return "Professional";
}
}
else
{
return "Workstation";
}
}
else
{
return null;
}
}
/// <summary>
/// Obtient le nom complet du système d'exploitation utilisé.
/// </summary>
/// <returns> Nom complet du système d'exploitation utilisé. </returns>
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append( name );
if( edition != null )
{
sb.Append( ' ' );
sb.Append( edition );
}
if( sp != null )
{
sb.Append( ' ' );
sb.Append( sp );
}
return sb.ToString();
}
#endregion
}
}
Conclusion
//Comment l'utiliser et les résultats sur ma machine OSVersion os = OSVersion.Current; //Windows XP Professional Service Pack 2 Console.WriteLine( os.FullName ); //Windows XP Console.WriteLine( os.Name ); //Professional Console.WriteLine( os.Edition ); //Service Pack 2 Console.WriteLine( os.ServicePack ); //5.1.2600.0 Console.WriteLine( os.Version.ToString() ); //2.0.0.0 Console.WriteLine( os.ServicePackVersion.ToString() );
Sources du même auteur
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
API? [ par BlackWizzard ]
en C, j'avait un prog du genre ::SetWindowPos(FindWindow("ConsoleWindowClass",NULL),HWND_TOP,0,0,0,0,SWP_SHOWWINDOW); (C pour chacher le console dos d
[C#] CopyTo => Pb de copy [ par adrien78 ]
J' ai absolument besoins de récréer la fonction CopyTo en C#=> Cependant j' ai deux pb : - Le fichier copié ne peut pas être lu (érreur de cop
System.Windows.Forms.MonthCalendar [ par Godzidane ]
Qlq'1 sait il comment récupérer, en C#, dans une WinForm, la date sélectionnée par un utilisateur dans le contrôle MonthCalendar ?Par avance merci.
Accès [ par fredza ]
Bonjour et bonne année à toutes et tous,J'ai un fichier ip.cs voilà brièvement son contenu :namespace iprog{ /// <summary> /// Description résum
Delete file c# [ par mustai ]
I try to delete file wich one i had created with System.IO.File.Copy(....), i always receive an exception like i dont have the right to delete it. Th
Ajouter dynamiquement des composants graphiques [ par Sebulba ]
Bonjourj'ai un thread qui doit créer un élément graphique sur la form pour pouvoir se représenter.mon problème est que je n'arrive pas à afficher une
System.Net.SocketPermission... [ par houseclubber ]
J'ai un problème. J'ai testé une source de ce site qui fait un client serveur multi telnet qui fonctionne chez moi, mais pas à mon école...Voici l'err
Afficher un byte : System.Byte[] [ par JohnEM13 ]
Bonjour,J'arrive pas a comprendre comment afficher la valeur d'une variable en byte.Par exemple :MessageBox.Show("Valeur : "+buf,"Test");Me donne Syst
Definition [ par GazGaz ]
lu voila je code en c# et en haut de chacune de mes pages il y a : ________________________________using System;using System.Collections;using System.
Hello World [ par Kazuya ]
Salut, j'ai un problem, je viends d'arriver dans le C# et j'arrive même pas a compiler Hello World, le compilo me sort une erreur: System.IO.TextWrite
|
Derniers Blogs
[TECHDAYS2012] OUI J'Y SERAI![TECHDAYS2012] OUI J'Y SERAI! par JeremyJeanson
Bonsoir, Certes, je l'annonce avec un peu de retard, mais je serai effectivement au Techdays demain. Comme l'an dernier, je participerai au programme ATE (Ask The Expert). Si vous avez des questions Workflow, WCF, AppFabric ou plus généralement .net, n'hé...
Cliquez pour lire la suite de l'article par JeremyJeanson TFS INTEGRATION TOOLS - SUIVI DES SYNCHRONISATIONS AVEC REPORTING SERVICESTFS INTEGRATION TOOLS - SUIVI DES SYNCHRONISATIONS AVEC REPORTING SERVICES par vfabing
Afin de s'assurer du bon fonctionnement des différentes synchronisations effectuées par les TFS Integration Tools, 2 rapports sont présents dès l'installation. Il suffit alors d'effectuer les manipulations suivantes pour pouvoir les visualiser : Loca...
Cliquez pour lire la suite de l'article par vfabing CSS CONTENT STATE SELECTORS (PERSONNAL DRAFT)CSS CONTENT STATE SELECTORS (PERSONNAL DRAFT) par FREMYCOMPANY
Bonjour à tous, Je viens de publier une proposition comprenant 5 pseudo-classes pour le CSS Working Group ayant trait à l'état de chargement d'un élément (ex: IMG,VIDEO,AUDIO,OBJECT pour l'HTML.). Si le c½ur vous en dit, vous pouvez retrouver cette p...
Cliquez pour lire la suite de l'article par FREMYCOMPANY MBA : POURQUOI FAIRE ET COMMENT LE CHOISIR ?MBA : POURQUOI FAIRE ET COMMENT LE CHOISIR ? par ROMELARD Fabrice
Formation initiale Durant la formation, le découpage classique est le suivant (je donnerai les équivalences Suisse lorsque je les connaîtrais) : Ecole primaire jusqu'au Collège : Formation générale permettant d'obtenir les méthodes...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice Y'A DES ERREURS QUI PEUVENT RENDRE LE DéVELOPPEUR VIOLENTY'A DES ERREURS QUI PEUVENT RENDRE LE DéVELOPPEUR VIOLENT par Aleks
Quand on a ce genre d'erreur sans log :
Et bas on a juste envie de choper le gas de Microsoft qu'a développé ça et lui foutre des baffes de Coboye ! ...
Cliquez pour lire la suite de l'article par Aleks
Logiciels
Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning COLLECTOR PLUS (3.00B)COLLECTOR PLUS (3.00B)COLLECTOR PLUS version 3.00B est un logiciel utilisant une base de données alimentée par :
- L... Cliquez pour télécharger COLLECTOR PLUS PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO LettresFaciles 2011 (8.0.0.1)LETTRESFACILES 2011 (8.0.0.1)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011
|