// Copyright (c) 2012, Yves Goergen, http://unclassified.de
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted
// provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this list of conditions
// and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice, this list of
// conditions and the following disclaimer in the documentation and/or other materials provided
// with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Win32;
namespace Unclassified
{
///
/// Provides information about the execution environment like the operating system version or
/// user context.
///
public class MyEnvironment
{
#region OS version detection
[StructLayout(LayoutKind.Sequential)]
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 ushort wSuiteMask;
public byte wProductType;
public byte wReserved;
}
private enum SystemMetric
{
SM_SERVERR2 = 89
}
private const ushort VER_SUITE_WH_SERVER = 0x8000;
private const ushort VER_NT_WORKSTATION = 1;
[DllImport("kernel32.dll")]
private static extern short GetVersionEx(ref OSVERSIONINFOEX osvi);
[DllImport("user32.dll")]
private static extern int GetSystemMetrics(SystemMetric smIndex);
private static OSVERSIONINFOEX osvi;
private static bool haveOsvi;
private static void EnsureVersion()
{
if (!haveOsvi)
{
osvi = new OSVERSIONINFOEX();
osvi.dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX));
GetVersionEx(ref osvi);
haveOsvi = true;
}
}
///
/// Gets the installed operating system version value.
///
public static OSVersion OSVersion
{
get
{
OperatingSystem os = Environment.OSVersion;
switch (os.Platform)
{
case PlatformID.Win32Windows:
if (os.Version.Major >= 4 && os.Version.Minor == 0)
return OSVersion.Windows95;
if (os.Version.Major >= 4 && os.Version.Minor > 0 && os.Version.Minor < 90)
return OSVersion.Windows98;
if (os.Version.Major >= 4 && os.Version.Minor >= 90)
return OSVersion.WindowsME;
break;
case PlatformID.Win32NT:
EnsureVersion();
int r2 = GetSystemMetrics(SystemMetric.SM_SERVERR2);
// Interpretation of the values see
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms724833.aspx
// section Remarks
if (os.Version.Major <= 4)
return OSVersion.WindowsNT4;
if (os.Version.Major == 5 && os.Version.Minor == 0)
return OSVersion.Windows2000;
if (os.Version.Major == 5 && os.Version.Minor == 1)
return OSVersion.WindowsXP;
if (os.Version.Major == 5 && os.Version.Minor == 2)
{
if ((osvi.wSuiteMask & VER_SUITE_WH_SERVER) != 0)
return OSVersion.WindowsHomeServer;
if (osvi.wProductType == VER_NT_WORKSTATION && Environment.Is64BitOperatingSystem)
return OSVersion.WindowsXP;
if (GetSystemMetrics(SystemMetric.SM_SERVERR2) == 0)
return OSVersion.WindowsServer2003;
if (GetSystemMetrics(SystemMetric.SM_SERVERR2) != 0)
return OSVersion.WindowsServer2003R2;
}
if (os.Version.Major == 6 && os.Version.Minor == 0)
{
if (osvi.wProductType == VER_NT_WORKSTATION)
return OSVersion.WindowsVista;
else
return OSVersion.WindowsServer2008;
}
if (os.Version.Major == 6 && os.Version.Minor == 1)
{
if (osvi.wProductType == VER_NT_WORKSTATION)
return OSVersion.Windows7;
else
return OSVersion.WindowsServer2008R2;
}
if (os.Version.Major == 6 && os.Version.Minor == 2)
{
if (osvi.wProductType == VER_NT_WORKSTATION)
return OSVersion.Windows8;
else
return OSVersion.WindowsServer2012;
}
if (os.Version.Major == 6 && os.Version.Minor > 2 || os.Version.Major > 6)
return OSVersion.WindowsFuture;
break;
}
return OSVersion.Unknown;
}
}
///
/// Gets the installed operating system name.
///
public static string OSName
{
get
{
switch (OSVersion)
{
case OSVersion.Windows95: return "Windows 95";
case OSVersion.Windows98: return "Windows 98";
case OSVersion.WindowsME: return "Windows ME";
case OSVersion.WindowsNT4: return "Windows NT 4";
case OSVersion.Windows2000: return "Windows 2000";
case OSVersion.WindowsXP: return "Windows XP";
case OSVersion.WindowsHomeServer: return "Windows Home Server";
case OSVersion.WindowsServer2003: return "Windows Server 2003";
case OSVersion.WindowsServer2003R2: return "Windows Server 2003 R2";
case OSVersion.WindowsVista: return "Windows Vista";
case OSVersion.WindowsServer2008: return "Windows Server 2008";
case OSVersion.Windows7: return "Windows 7";
case OSVersion.WindowsServer2008R2: return "Windows Server 2008 R2";
case OSVersion.Windows8: return "Windows 8";
case OSVersion.WindowsServer2012: return "Windows Server 2012";
case OSVersion.WindowsFuture: return "Future Windows version";
case OSVersion.Unknown: return "Unknown";
}
return "";
}
}
///
/// Gets a value indicating whether the running process is 64 bit.
///
public static bool Is64BitProcess
{
get
{
return Environment.Is64BitProcess;
//return IntPtr.Size == 8;
}
}
///
/// Gets a value indicating whether the installed operating system is 64 bit.
///
public static bool Is64BitOS
{
get
{
return Environment.Is64BitOperatingSystem;
}
}
///
/// Gets the installed operating system product name, including edition.
///
public static string OSProductName
{
get
{
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
try
{
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion"))
{
string productName = key.GetValue("ProductName") as string;
if (string.IsNullOrEmpty(productName))
{
// Missing value, use fallback
return OSName;
}
return productName;
}
}
catch
{
// Something went wrong, use fallback
return OSName;
}
}
else
{
// Not an NT system, use fallback
return OSName;
}
}
}
///
/// Gets the major version number of the installed service pack, if any, or 0 otherwise.
///
static public int OSServicePack
{
get
{
EnsureVersion();
return osvi.wServicePackMajor;
}
}
///
/// Gets the name of the installed service pack, if any, or an empty string otherwise.
///
static public string OSServicePackString
{
get
{
EnsureVersion();
return osvi.szCSDVersion;
}
}
///
/// Gets the installed operating system build number.
///
public static string OSBuild
{
get
{
OperatingSystem os = Environment.OSVersion;
return os.Version.Build.ToString();
}
}
#endregion OS version detection
#region Runtime detection
///
/// Gets the type of the CLR runtime, Mono or .NET.
///
public static string ClrRuntimeType
{
get
{
if (Type.GetType("Mono.Runtime") != null)
return "Mono";
return "Microsoft .NET";
}
}
#endregion Runtime detection
#region OS user detection
///
/// Checks whether the logged on Windows user is member of the specified Windows group.
///
/// Group name (format: "Domain\Group")
/// true, if the user is member of the group, false otherwise.
/// Source: http://www.mycsharp.de/wbb2/thread.php?threadid=36895
public static bool IsCurrentUserInWindowsGroup(string groupName)
{
try
{
System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
if (!identity.IsAuthenticated)
throw new System.Security.SecurityException("The current Windows user is not authenticated.");
System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
return principal.IsInRole(groupName);
}
catch (SystemException)
{
// The group is not found. If the group is not created then
// the user cannot be member of the group so just return false.
return false;
}
}
#endregion OS user detection
#region Application information
public static string AssemblyTitle
{
get
{
object[] customAttributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
if (customAttributes != null && customAttributes.Length > 0)
{
return ((AssemblyTitleAttribute) customAttributes[0]).Title;
}
return null;
}
}
public static string AssemblyProduct
{
get
{
object[] customAttributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
if (customAttributes != null && customAttributes.Length > 0)
{
return ((AssemblyProductAttribute) customAttributes[0]).Product;
}
return null;
}
}
public static string AssemblyFileVersion
{
get
{
object[] customAttributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false);
if (customAttributes != null && customAttributes.Length > 0)
{
return ((AssemblyFileVersionAttribute) customAttributes[0]).Version;
}
return null;
}
}
public static string AssemblyDescription
{
get
{
object[] customAttributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
if (customAttributes != null && customAttributes.Length > 0)
{
return ((AssemblyDescriptionAttribute) customAttributes[0]).Description;
}
return null;
}
}
public static string AssemblyCopyright
{
get
{
object[] customAttributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
if (customAttributes != null && customAttributes.Length > 0)
{
return ((AssemblyCopyrightAttribute) customAttributes[0]).Copyright;
}
return null;
}
}
public static string AssemblyInformationalVersion
{
get
{
object[] customAttributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false);
if (customAttributes != null && customAttributes.Length > 0)
{
return ((AssemblyInformationalVersionAttribute) customAttributes[0]).InformationalVersion;
}
return null;
}
}
#endregion Application information
}
#region Operating system versions enumeration
///
/// Enumeration of known operating systems
///
public enum OSVersion
{
///
/// Unknown
///
Unknown,
///
/// Microsoft Windows 95
///
Windows95,
///
/// Microsoft Windows 98
///
Windows98,
///
/// Microsoft Windows ME
///
WindowsME,
///
/// Microsoft Windows NT 4.0
///
WindowsNT4,
///
/// Microsoft Windows 2000
///
Windows2000,
///
/// Microsoft Windows XP
///
WindowsXP,
///
/// Microsoft Windows Home Server (like Windows XP)
///
WindowsHomeServer,
///
/// Microsoft Windows Server 2003 (like Windows XP)
///
WindowsServer2003,
///
/// Microsoft Windows Server 2003 R2 (like Windows XP)
///
WindowsServer2003R2,
///
/// Microsoft Windows Vista
///
WindowsVista,
///
/// Microsoft Windows Server 2008 (like Windows Vista)
///
WindowsServer2008,
///
/// Microsoft Windows 7
///
Windows7,
///
/// Microsoft Windows Server 2008 R2 (like Windows 7)
///
WindowsServer2008R2,
///
/// Microsoft Windows 8
///
Windows8,
///
/// Microsoft Windows Server 2012 (like Windows 8)
///
WindowsServer2012,
///
/// Future version of Microsoft Windows
///
WindowsFuture
}
#endregion Operating system versions enumeration
}