505 lines
16 KiB
C#
505 lines
16 KiB
C#
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
|
|
/// <summary>
|
|
/// Contains the name of the program file
|
|
/// </summary>
|
|
public static string DllName { set; get; }
|
|
|
|
/// <summary>
|
|
/// Contains the version of the program file
|
|
/// </summary>
|
|
public static string DllVersion { set; get; }
|
|
|
|
/// <summary>
|
|
/// Contains the copyright notice
|
|
/// </summary>
|
|
public static string Copyright { set; get; }
|
|
|
|
private static void SetDllInfo()
|
|
{
|
|
//Name, Version und Copyright setzen
|
|
DllName = dllName;
|
|
DllVersion = dllVersion;
|
|
Copyright = copyright;
|
|
}
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// Information about Windows
|
|
/// </summary>
|
|
static WindowsInfo()
|
|
{
|
|
SetDllInfo(); //Name, Version und Copyright setzen
|
|
CollectWindowsInfomations();
|
|
}
|
|
|
|
#region Globale_Variablen
|
|
/// <summary>
|
|
/// Returns the installed operating system. E.g.: Microsoft Windows 10 Enterprise - 64-Bit
|
|
/// </summary>
|
|
public static string OS { set; get; }
|
|
|
|
/// <summary>
|
|
/// Returns the product name of the installed operating system. E.g.: Windows 10 Enterprise
|
|
/// </summary>
|
|
public static string ProductName { set; get; }
|
|
|
|
/// <summary>
|
|
/// Returns the edition ID of the installed operating system. E.g.: Enterprise
|
|
/// </summary>
|
|
public static string EditionId { set; get; }
|
|
|
|
/// <summary>
|
|
/// Returns the current version of the installed operating system. E.g.: 6.3
|
|
/// </summary>
|
|
public static string CurrentVersion { set; get; }
|
|
|
|
/// <summary>
|
|
/// Returns the current release ID of the installed operating system. E.g.: 1709
|
|
/// </summary>
|
|
public static string ReleaseId { set; get; }
|
|
|
|
/// <summary>
|
|
/// Returns the current build number of the installed operating system. E.g.: 16299
|
|
/// </summary>
|
|
public static string CurrentBuildNumber { set; get; }
|
|
|
|
/// <summary>
|
|
/// Returns the build lab of the installed operating system. E.g.: 16299.431
|
|
/// </summary>
|
|
public static string BuildLab { set; get; }
|
|
|
|
/// <summary>
|
|
/// Returns the operating system name of the installed operating system. E.g.: Microsoft Windows 10 Enterprise
|
|
/// </summary>
|
|
public static string OSName { set; get; }
|
|
|
|
/// <summary>
|
|
/// Returns the OS version string of the installed operating system. E.g.: Microsoft Windows NT 6.2.9200.0
|
|
/// </summary>
|
|
public static string OSVersionString { set; get; }
|
|
|
|
/// <summary>
|
|
/// Returns the OS version of the installed operating system. E.g.: 6.2.9200.0
|
|
/// </summary>
|
|
public static string OSVersion { set; get; }
|
|
|
|
/// <summary>
|
|
/// Returns the OS architecture of the installed operating system. E.g.: 64-Bit
|
|
/// </summary>
|
|
public static string OSArchitecture { set; get; }
|
|
|
|
/// <summary>
|
|
/// Returns the OS service pack of the installed operating system. E.g.: SP1
|
|
/// </summary>
|
|
public static string OSServicePack { set; get; }
|
|
|
|
/// <summary>
|
|
/// Returns the OS major version of the installed operating system. E.g.: 6
|
|
/// </summary>
|
|
public static int OSMajorVersion { set; get; }
|
|
|
|
/// <summary>
|
|
/// Returns the OS minor version of the installed operating system. E.g.: 2
|
|
/// </summary>
|
|
public static int OSMinorVersion { set; get; }
|
|
|
|
/// <summary>
|
|
/// Returns the OS major revision of the installed operating system. E.g.: 0
|
|
/// </summary>
|
|
public static int OSMajorRevision { set; get; }
|
|
|
|
/// <summary>
|
|
/// Returns the OS minor revision of the installed operating system. E.g.: 0
|
|
/// </summary>
|
|
public static int OSMinorRevision { set; get; }
|
|
|
|
/// <summary>
|
|
/// Returns the OS build of the installed operating system. E.g.: 9200
|
|
/// </summary>
|
|
public static int OSBuild { set; get; }
|
|
|
|
/// <summary>
|
|
/// Returns the process architecture of the installed operating system. E.g.: Win32NT
|
|
/// </summary>
|
|
public static string ProcessArchitecture { set; get; }
|
|
#endregion
|
|
|
|
OperatingSystem os = Environment.OSVersion;
|
|
|
|
#region Methoden
|
|
/// <summary>
|
|
/// Collects all Windows informations
|
|
/// </summary>
|
|
/// <exception cref="UnableToReadOSInformationException">If a value cannot be read, an exception is thrown</exception>
|
|
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();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Get the installed operating system.
|
|
/// </summary>
|
|
/// <returns>The installed OS. E.g.: Microsoft Windows 10 Enterprise - 64-Bit</returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the product name of the installed operating system.
|
|
/// </summary>
|
|
/// <returns>The product name. E.g.: Windows 10 Enterprise</returns>
|
|
private static string GetProductName()
|
|
{
|
|
var regKey = Registry.LocalMachine;
|
|
var subKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", false);
|
|
return subKey.GetValue("ProductName").ToString();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the edition ID of the installed operating system.
|
|
/// </summary>
|
|
/// <returns>The edition ID. E.g.: Enterprise</returns>
|
|
private static string GetEditionId()
|
|
{
|
|
var regKey = Registry.LocalMachine;
|
|
var subKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", false);
|
|
return subKey.GetValue("EditionID").ToString();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the current version of the installed operating system.
|
|
/// </summary>
|
|
/// <returns>The current version. E.g.: 6.3</returns>
|
|
private static string GetCurrentVersion()
|
|
{
|
|
var regKey = Registry.LocalMachine;
|
|
var subKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", false);
|
|
return subKey.GetValue("CurrentVersion").ToString();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the current release ID of the installed operating system.
|
|
/// </summary>
|
|
/// <returns>The current release ID. E.g.: 1709</returns>
|
|
private static string GetReleaseId()
|
|
{
|
|
var regKey = Registry.LocalMachine;
|
|
var subKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", false);
|
|
return subKey.GetValue("ReleaseId").ToString();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the current build number of the installed operating system.
|
|
/// </summary>
|
|
/// <returns>The current build number. E.g.: 16299</returns>
|
|
private static string GetCurrentBuildNumber()
|
|
{
|
|
var regKey = Registry.LocalMachine;
|
|
var subKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", false);
|
|
return subKey.GetValue("CurrentBuildNumber").ToString();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the build lab of the installed operating system.
|
|
/// </summary>
|
|
/// </summary>
|
|
/// <returns>The build lab. E.g.: 16299.431</returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the operating system name of the installed operating system.
|
|
/// </summary>
|
|
/// <returns>The OS name. E.g.: Microsoft Windows 10 Enterprise</returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the OS version string of the installed operating system.
|
|
/// </summary>
|
|
/// <returns>The OS version string. E.g.: Microsoft Windows NT 6.2.9200.0</returns>
|
|
private static string GetOSVersionString()
|
|
{
|
|
return Environment.OSVersion.VersionString;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the OS version of the installed operating system.
|
|
/// </summary>
|
|
/// <returns>The OS version. E.g.: 6.2.9200.0</returns>
|
|
private static string GetOSVersion()
|
|
{
|
|
return Environment.OSVersion.Version.ToString();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the OS architecture of the installed operating system.
|
|
/// </summary>
|
|
/// <returns>The OS architecture. E.g.: 64-Bit</returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the OS service pack of the installed operating system.
|
|
/// </summary>
|
|
/// <returns>The OS service pack. E.g.: SP1</returns>
|
|
private static string GetOSServicePack()
|
|
{
|
|
return Environment.OSVersion.ServicePack;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the OS major version of the installed operating system.
|
|
/// </summary>
|
|
/// <returns>The OS major version. E.g.: 6</returns>
|
|
private static int GetOSMajorVersion()
|
|
{
|
|
Version ver = Environment.OSVersion.Version;
|
|
return ver.Major;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the OS minor version of the installed operating system.
|
|
/// </summary>
|
|
/// <returns>The OS minor version. E.g.: 2</returns>
|
|
private static int GetOSMinorVersion()
|
|
{
|
|
Version ver = Environment.OSVersion.Version;
|
|
return ver.Minor;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the OS major revision of the installed operating system.
|
|
/// </summary>
|
|
/// <returns>The OS major revision. E.g.: 0</returns>
|
|
private static int GetOSMajorRevision()
|
|
{
|
|
Version ver = Environment.OSVersion.Version;
|
|
return ver.MajorRevision;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the OS minor revision of the installed operating system.
|
|
/// </summary>
|
|
/// <returns>The OS minor revision. E.g.: 0</returns>
|
|
private static int GetOSMinorRevision()
|
|
{
|
|
Version ver = Environment.OSVersion.Version;
|
|
return ver.MinorRevision;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the OS build of the installed operating system.
|
|
/// </summary>
|
|
/// <returns>The OS build. E.g.: 9200</returns>
|
|
private static int GetOSBuild()
|
|
{
|
|
Version ver = Environment.OSVersion.Version;
|
|
return ver.Build;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the process architecture of the installed operating system.
|
|
/// </summary>
|
|
/// <returns>The process architecture. E.g.: Win32NT</returns>
|
|
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
|
|
}
|