c#


Identify the Operating System details in C#


How can I get the OS details using C# code in my WPF application?
The Environment class provides properties that can be used to obtain system information.
You can get OS information from System. Environment.OSVersion Here
Have a look at System.Environment It has property OSVersion
Since I only have to care about non server editions, I do:
enum OS { _2000, XP, Vista, _7, _8 }
public static OS GetOS()
{
var version = Environment.OSVersion.Version;
switch (version.Major)
{
case 5:
switch (version.Minor)
{
case 0:
return OS._2000;
case 1:
return OS.XP;
case 2:
return OS.XP; //could also be Server 2003, Server 2003 R2
}
break;
case 6:
switch (version.Minor)
{
case 0:
return OS.Vista; //could also be Server 2008
case 1:
return OS._7; //could also be Server 2008 R2
case 2:
return OS._8; //could also be Server 20012, Server 2012 R2
}
break;
}
throw new Exception("Strange OS");
}
If you really need to consider server editions as well, then your options are:
WMI, you will have to some manual parsing. Not sure if user privilege is going to hurt non admin users.
GetVersionEx as described in this answer.
Checking for ProductName at
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\
IsOS function, as described in this answer. Of all I prefer this..
I provided a bit more complete answer here.
Using the registry is something that is possible for any application. In C# I have made myself a utility class for this. Note that Microsoft has changed the key for Windows 10+ (which this class is designed to already handle). The class should give you the information you need, I think:
namespace Inspection
{
/// <summary>
/// Static class that adds convenient methods for getting information on the running computers basic hardware and os setup.
/// </summary>
public static class ComputerInfo
{
/// <summary>
/// Returns the Windows major version number for this computer.
/// </summary>
public static uint WinMajorVersion
{
get
{
dynamic major;
// The 'CurrentMajorVersionNumber' string value in the CurrentVersion key is new for Windows 10,
// and will most likely (hopefully) be there for some time before MS decides to change this - again...
if (TryGeRegistryKey(#"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentMajorVersionNumber", out major))
{
return (uint) major;
}
// When the 'CurrentMajorVersionNumber' value is not present we fallback to reading the previous key used for this: 'CurrentVersion'
dynamic version;
if (!TryGeRegistryKey(#"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion", out version))
return 0;
var versionParts = ((string) version).Split('.');
if (versionParts.Length != 2) return 0;
uint majorAsUInt;
return uint.TryParse(versionParts[0], out majorAsUInt) ? majorAsUInt : 0;
}
}
/// <summary>
/// Returns the Windows minor version number for this computer.
/// </summary>
public static uint WinMinorVersion
{
get
{
dynamic minor;
// The 'CurrentMinorVersionNumber' string value in the CurrentVersion key is new for Windows 10,
// and will most likely (hopefully) be there for some time before MS decides to change this - again...
if (TryGeRegistryKey(#"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentMinorVersionNumber",
out minor))
{
return (uint) minor;
}
// When the 'CurrentMinorVersionNumber' value is not present we fallback to reading the previous key used for this: 'CurrentVersion'
dynamic version;
if (!TryGeRegistryKey(#"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion", out version))
return 0;
var versionParts = ((string) version).Split('.');
if (versionParts.Length != 2) return 0;
uint minorAsUInt;
return uint.TryParse(versionParts[1], out minorAsUInt) ? minorAsUInt : 0;
}
}
/// <summary>
/// Returns whether or not the current computer is a server or not.
/// </summary>
public static uint IsServer
{
get
{
dynamic installationType;
if (TryGeRegistryKey(#"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "InstallationType",
out installationType))
{
return (uint) (installationType.Equals("Client") ? 0 : 1);
}
return 0;
}
}
private static bool TryGeRegistryKey(string path, string key, out dynamic value)
{
value = null;
try
{
var rk = Registry.LocalMachine.OpenSubKey(path);
if (rk == null) return false;
value = rk.GetValue(key);
return value != null;
}
catch
{
return false;
}
}
}
}

Related Links

GoogleConnect.Fetch throws Unable to cast object error
Sum method return long value
Why doesn't my code access the control inside item template?
Trying to add rows to a Table results in multiple tables
Getting error “Unable to load DLL 'SQLite.Interop.dll'” despite it being in the bin folder
Append data in a json file in C#
C# Foreach loop through json-object, get key [duplicate]
Excel Connectionstring DriverId
BotConnector dynamic field not working: even when a user clicks on one of the buttons in the prompt, the Bot fails to recognize the response
WCF Collection returns empty
Extending interface with new model
How to programmatically get Azure Service Bus Relay details (C#)?
The type or namespace name 'ThreadPoolTimer' could not be found
Join IQueryable with List on object from list
Exporting gridview with paging
Is it possible to pin a text on the top of the console c# [duplicate]

Categories

HOME
max
logging
model-view-controller
redux
netty
gsm
postgresql-9.3
weblogic
liquibase
highmaps
localhost
sahi
google-app-maker
runtime
adroitlogic
hdf5
lambda-calculus
web-worker
caldav
matlab-gui
concatenation
aspxgridview
oracle10g
rancher
adobe-captivate
csproj
request-uri
key-bindings
ios-autolayout
tikz
ansible-inventory
pymunk
repeat
javafxports
nsdateformatter
gem-fury
ssdp
compiled-query
gradlew
reqif
jenkins-2
dcom
bioperl
programming-pearls
fileopendialog
ownership
netlink
user-profile
razorengine
treelist
radar-chart
wordpress-plugin
fadein
newlib
mojolicious
javapackager
gulp-cdnizer
play-json
database-project
lytro
adempiere
vensim
playn
clearquest
teamwork
esb-toolkit-2.1
jeet-grid
yosemite
lwuit
zoneminder
powershell-v1.0
agent-based-modeling
javacompiler
ios-enterprise
nest-initiative
sigma-grid-control
mahara
maven-javadoc-plugin
friendly-id
xrandr
friendly-url
kademlia
panelgrid
azure-scheduler
vline
ncml
libx264
tasklist
doxia
xcode4.6.3
shapado
waveout
sup
wxperl
mvcmailer
jmesa
pinchzoom
cherokee
backlight
jqtransform
cpack
noise-reduction
netbeans7.0
mass-emails
nscharacterset
oaw
cons

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App