using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
namespace Eugen.ESystem.Windows.Info
{
public class WindowsInfo
{
#region Version und Copyright
// Version und Copyright
static string dllName = Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); //Den Programmnamen auslesen
static string dllVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
static object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
static string copyright = GenerateCopyright();
//Assembly Datum und Zeit
static DateTime value = AssemblyDateTime();
static string date = value.ToShortDateString();
static string time = value.ToLongTimeString();
private static DateTime AssemblyDateTime()
{
var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
var buildDateTime = new DateTime(2000, 1, 1).Add(new TimeSpan(TimeSpan.TicksPerDay * version.Build + TimeSpan.TicksPerSecond * 2 * version.Revision));
//Tage seit dem 1. Januar 2000 und Sekunden seit Mitternacht, (multiplizier mit 2 ergibt das Original)
return buildDateTime;
}
private static string CurrentYear()
{
DateTime dtn = DateTime.Now;
return dtn.Year.ToString();
}
private static string StartYear()
{
string startYear = "";
if (((AssemblyCopyrightAttribute)attributes[0]).Copyright.Contains("Copyright © "))
{
startYear = ((AssemblyCopyrightAttribute)attributes[0]).Copyright.Replace("Copyright © ", "");
while (startYear.StartsWith(" "))
{
startYear = startYear.Remove(0, 1);
}
if (startYear.Contains("-"))
{
startYear = startYear.Remove(startYear.IndexOf("-"));
}
else
{
startYear = startYear.Remove(startYear.IndexOf(" "));
}
return startYear;
}
else
{
DateTime dtn = DateTime.Now;
return dtn.Year.ToString();
}
}
private static string GenerateCopyright()
{
string startYear = StartYear();
string currentYear = CurrentYear();
if (startYear == currentYear)
{
return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
}
else
{
string temp = ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
string start = temp.Remove(temp.IndexOf(startYear) - 1);
string end = (temp.Remove(0, temp.IndexOf(" by")));
return String.Concat(start, " ", startYear, "-", currentYear, end);
}
}
#endregion
#region DLL-Info
///
/// Contains the name of the program file
///
public static string DllName { set; get; }
///
/// Contains the version of the program file
///
public static string DllVersion { set; get; }
///
/// Contains the copyright notice
///
public static string Copyright { set; get; }
private static void SetDllInfo()
{
//Name, Version und Copyright setzen
DllName = dllName;
DllVersion = dllVersion;
Copyright = copyright;
}
#endregion
///
/// Information about Windows
///
static WindowsInfo()
{
SetDllInfo(); //Name, Version und Copyright setzen
CollectWindowsInfomations();
}
#region Globale_Variablen
///
/// Returns the installed operating system. E.g.: Microsoft Windows 10 Enterprise - 64-Bit
///
public static string OS { set; get; }
///
/// Returns the product name of the installed operating system. E.g.: Windows 10 Enterprise
///
public static string ProductName { set; get; }
///
/// Returns the edition ID of the installed operating system. E.g.: Enterprise
///
public static string EditionId { set; get; }
///
/// Returns the current version of the installed operating system. E.g.: 6.3
///
public static string CurrentVersion { set; get; }
///
/// Returns the current release ID of the installed operating system. E.g.: 1709
///
public static string ReleaseId { set; get; }
///
/// Returns the current build number of the installed operating system. E.g.: 16299
///
public static string CurrentBuildNumber { set; get; }
///
/// Returns the build lab of the installed operating system. E.g.: 16299.431
///
public static string BuildLab { set; get; }
///
/// Returns the operating system name of the installed operating system. E.g.: Microsoft Windows 10 Enterprise
///
public static string OSName { set; get; }
///
/// Returns the OS version string of the installed operating system. E.g.: Microsoft Windows NT 6.2.9200.0
///
public static string OSVersionString { set; get; }
///
/// Returns the OS version of the installed operating system. E.g.: 6.2.9200.0
///
public static string OSVersion { set; get; }
///
/// Returns the OS architecture of the installed operating system. E.g.: 64-Bit
///
public static string OSArchitecture { set; get; }
///
/// Returns the OS service pack of the installed operating system. E.g.: SP1
///
public static string OSServicePack { set; get; }
///
/// Returns the OS major version of the installed operating system. E.g.: 6
///
public static int OSMajorVersion { set; get; }
///
/// Returns the OS minor version of the installed operating system. E.g.: 2
///
public static int OSMinorVersion { set; get; }
///
/// Returns the OS major revision of the installed operating system. E.g.: 0
///
public static int OSMajorRevision { set; get; }
///
/// Returns the OS minor revision of the installed operating system. E.g.: 0
///
public static int OSMinorRevision { set; get; }
///
/// Returns the OS build of the installed operating system. E.g.: 9200
///
public static int OSBuild { set; get; }
///
/// Returns the process architecture of the installed operating system. E.g.: Win32NT
///
public static string ProcessArchitecture { set; get; }
#endregion
OperatingSystem os = Environment.OSVersion;
#region Methoden
///
/// Collects all Windows informations
///
/// If a value cannot be read, an exception is thrown
public static void CollectWindowsInfomations()
{
try
{
OS = GetOS();
ProductName = GetProductName();
EditionId = GetEditionId();
CurrentVersion = GetCurrentVersion();
ReleaseId = GetReleaseId();
CurrentBuildNumber = GetCurrentBuildNumber();
BuildLab = GetBuildLab();
OSName = GetOSName();
OSVersionString = GetOSVersionString();
OSVersion = GetOSVersion();
OSArchitecture = GetOSArchitecture();
OSServicePack = GetOSServicePack();
OSMajorVersion = GetOSMajorVersion();
OSMinorVersion = GetOSMinorVersion();
OSMajorRevision = GetOSMajorRevision();
OSMinorRevision = GetOSMinorRevision();
OSBuild = GetOSBuild();
ProcessArchitecture = GetProcessArchitecture();
}
catch (Exception)
{
throw new UnableToReadOSInformationException();
}
}
///
/// Get the installed operating system.
///
/// The installed OS. E.g.: Microsoft Windows 10 Enterprise - 64-Bit
private static string GetOS()
{
string r = "";
using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem"))
{
ManagementObjectCollection information = searcher.Get();
if (information != null)
{
foreach (ManagementObject obj in information)
{
r = obj["Caption"].ToString() + " - " + obj["OSArchitecture"].ToString();
}
}
r = r.Replace("NT 5.1.2600", "XP");
r = r.Replace("NT 5.2.3790", "Server 2003");
}
return r;
}
///
/// Get the product name of the installed operating system.
///
/// The product name. E.g.: Windows 10 Enterprise
private static string GetProductName()
{
var regKey = Registry.LocalMachine;
var subKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", false);
return subKey.GetValue("ProductName").ToString();
}
///
/// Get the edition ID of the installed operating system.
///
/// The edition ID. E.g.: Enterprise
private static string GetEditionId()
{
var regKey = Registry.LocalMachine;
var subKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", false);
return subKey.GetValue("EditionID").ToString();
}
///
/// Get the current version of the installed operating system.
///
/// The current version. E.g.: 6.3
private static string GetCurrentVersion()
{
var regKey = Registry.LocalMachine;
var subKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", false);
return subKey.GetValue("CurrentVersion").ToString();
}
///
/// Get the current release ID of the installed operating system.
///
/// The current release ID. E.g.: 1709
private static string GetReleaseId()
{
var regKey = Registry.LocalMachine;
var subKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", false);
return subKey.GetValue("ReleaseId").ToString();
}
///
/// Get the current build number of the installed operating system.
///
/// The current build number. E.g.: 16299
private static string GetCurrentBuildNumber()
{
var regKey = Registry.LocalMachine;
var subKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", false);
return subKey.GetValue("CurrentBuildNumber").ToString();
}
///
/// Get the build lab of the installed operating system.
///
///
/// The build lab. E.g.: 16299.431
private static string GetBuildLab()
{
var regKey = Registry.LocalMachine;
var subKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", false);
string key = subKey.GetValue("BuildLabEx").ToString();
string value = key.Remove(key.IndexOf(".") + 1);
key = key.Remove(0, key.IndexOf(".") + 1);
value = String.Concat(value, key.Remove(key.IndexOf(".")));
return value;
}
///
/// Get the operating system name of the installed operating system.
///
/// The OS name. E.g.: Microsoft Windows 10 Enterprise
private static string GetOSName()
{
string r = "";
using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem"))
{
ManagementObjectCollection information = searcher.Get();
if (information != null)
{
foreach (ManagementObject obj in information)
{
r = obj["Caption"].ToString();
}
}
r = r.Replace("NT 5.1.2600", "XP");
r = r.Replace("NT 5.2.3790", "Server 2003");
}
return r;
}
///
/// Get the OS version string of the installed operating system.
///
/// The OS version string. E.g.: Microsoft Windows NT 6.2.9200.0
private static string GetOSVersionString()
{
return Environment.OSVersion.VersionString;
}
///
/// Get the OS version of the installed operating system.
///
/// The OS version. E.g.: 6.2.9200.0
private static string GetOSVersion()
{
return Environment.OSVersion.Version.ToString();
}
///
/// Get the OS architecture of the installed operating system.
///
/// The OS architecture. E.g.: 64-Bit
private static string GetOSArchitecture()
{
string r = "";
using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem"))
{
ManagementObjectCollection information = searcher.Get();
if (information != null)
{
foreach (ManagementObject obj in information)
{
r = obj["OSArchitecture"].ToString();
}
}
}
return r;
}
///
/// Get the OS service pack of the installed operating system.
///
/// The OS service pack. E.g.: SP1
private static string GetOSServicePack()
{
return Environment.OSVersion.ServicePack;
}
///
/// Get the OS major version of the installed operating system.
///
/// The OS major version. E.g.: 6
private static int GetOSMajorVersion()
{
Version ver = Environment.OSVersion.Version;
return ver.Major;
}
///
/// Get the OS minor version of the installed operating system.
///
/// The OS minor version. E.g.: 2
private static int GetOSMinorVersion()
{
Version ver = Environment.OSVersion.Version;
return ver.Minor;
}
///
/// Get the OS major revision of the installed operating system.
///
/// The OS major revision. E.g.: 0
private static int GetOSMajorRevision()
{
Version ver = Environment.OSVersion.Version;
return ver.MajorRevision;
}
///
/// Get the OS minor revision of the installed operating system.
///
/// The OS minor revision. E.g.: 0
private static int GetOSMinorRevision()
{
Version ver = Environment.OSVersion.Version;
return ver.MinorRevision;
}
///
/// Get the OS build of the installed operating system.
///
/// The OS build. E.g.: 9200
private static int GetOSBuild()
{
Version ver = Environment.OSVersion.Version;
return ver.Build;
}
///
/// Get the process architecture of the installed operating system.
///
/// The process architecture. E.g.: Win32NT
private static string GetProcessArchitecture()
{
return Environment.OSVersion.Platform.ToString();
}
#endregion
}
#region Exceptions
public class UnableToReadOSInformationException : ApplicationException
{
public UnableToReadOSInformationException()
{ }
public UnableToReadOSInformationException(string message) : base(message)
{ }
public UnableToReadOSInformationException(string message, Exception inner) : base(message, inner)
{ }
}
public class UnknownErrorException : ApplicationException
{
public UnknownErrorException()
{ }
public UnknownErrorException(string message) : base(message)
{ }
public UnknownErrorException(string message, Exception inner) : base(message, inner)
{ }
}
#endregion
}