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
UNE JOLIE-HORLOGE ET PAS QU'UN PEU !UNE JOLIE-HORLOGE ET PAS QU'UN PEU ! par neodante
Pour les possesseurs d'iPhone, ça y est Bijin Tokei - qui se traduit littéralement en Français par " Jolie Horloge " - est arrivé et GRATUITEMENT s'il vous plaît ! Après la version Tokyo, Hokkaido, night club, racing, Gal, "pour les mademoiselles'", . voi...
Cliquez pour lire la suite de l'article par neodante TECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICESTECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICES par ROMELARD Fabrice
Animé par: Gaetan Bouveret et Julien Chomarat Business Connectivity Services (BCS) est dans SharePoint 2010 la version 2 de Business Data Catalog (BDC dans SharePoint 2007). Il s'agit de la solution permettant de visualiser des données provenan...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice [DIVERS] SUIVRE VOS SéRIES PRéFéRéS SUR LA TOILE[DIVERS] SUIVRE VOS SéRIES PRéFéRéS SUR LA TOILE par orion
Comme de nombreux geek, je suis un grand amateur de série TV et je rate régulièrement des épisodes de mes séries préférés. Une solution s'offre à vous avec ce merveilleux site : Tv Gorge - www.tvgorge.com Moteur de recherche à l'appui, vous pouvez ...
Cliquez pour lire la suite de l'article par orion TECHDAYS PARIS 2010 : LA BI DANS SHAREPOINT 2010TECHDAYS PARIS 2010 : LA BI DANS SHAREPOINT 2010 par ROMELARD Fabrice
Animé par: Vincent Bellet et Baptiste Giraudier La BI dans SharePoint 2010, Les nouveaux services d'application dans SP2010 et SQL Server Reporting services 2008 R2. La BI dans SharePoint est généralisée pour tous afin de permettre à tous les coll...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Logiciels
DB-MAIN (9.1.0)DB-MAIN (9.1.0)DB-MAIN is a data-modeling and data-architecture tool. It is designed to help developers and anal... Cliquez pour télécharger DB-MAIN Xilisoft DPG Convertisseur (5.1.37.0120)XILISOFT DPG CONVERTISSEUR (5.1.37.0120)Xilisoft DPG Convertisseur offre aux fans de Nintendo DS une bonne solution leur permettant de dé... Cliquez pour télécharger Xilisoft DPG Convertisseur GraphicsGale (2.01.01)GRAPHICSGALE (2.01.01)GraphicsGale est un logiciel de PixelArt avec de nombreuse fonctionnalités permettant de réalisé ... Cliquez pour télécharger GraphicsGale Architecte 3D (Platinum 2010)ARCHITECTE 3D (PLATINUM 2010)Architecte 3D Platinium vous permet de concevoir facilement les plans votre future maison, de l'é... Cliquez pour télécharger Architecte 3D TeamViewer 5 (TeamViewer 5)TEAMVIEWER 5 (TEAMVIEWER 5)Dépanner un ami,expliquer une manipulation devient un jeu d'enfant.
Prise en main d'un autre ord... Cliquez pour télécharger TeamViewer 5
|