2017-08-14 17:44:16 +02:00
|
|
|
|
/*
|
2024-03-05 18:37:42 +01:00
|
|
|
|
* Copyright (c) 2024 ETH Zürich, IT Services
|
2017-08-14 17:44:16 +02:00
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-12-20 17:06:28 +01:00
|
|
|
|
using System;
|
2020-05-06 21:45:04 +02:00
|
|
|
|
using System.Collections.Generic;
|
2023-09-01 12:28:03 +02:00
|
|
|
|
using System.IO;
|
2019-12-20 17:06:28 +01:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Management;
|
2017-08-14 17:44:16 +02:00
|
|
|
|
using System.Windows.Forms;
|
2019-08-30 09:55:26 +02:00
|
|
|
|
using SafeExamBrowser.SystemComponents.Contracts;
|
2023-09-01 12:28:03 +02:00
|
|
|
|
using SafeExamBrowser.SystemComponents.Contracts.Registry;
|
2019-01-23 15:57:49 +01:00
|
|
|
|
using BatteryChargeStatus = System.Windows.Forms.BatteryChargeStatus;
|
2019-12-20 17:06:28 +01:00
|
|
|
|
using OperatingSystem = SafeExamBrowser.SystemComponents.Contracts.OperatingSystem;
|
2017-08-14 17:44:16 +02:00
|
|
|
|
|
2019-01-23 15:57:49 +01:00
|
|
|
|
namespace SafeExamBrowser.SystemComponents
|
2017-08-14 17:44:16 +02:00
|
|
|
|
{
|
|
|
|
|
public class SystemInfo : ISystemInfo
|
|
|
|
|
{
|
2023-09-01 12:28:03 +02:00
|
|
|
|
private readonly IRegistry registry;
|
|
|
|
|
|
2022-07-29 13:49:26 +02:00
|
|
|
|
public string BiosInfo { get; private set; }
|
2023-07-31 10:52:40 +02:00
|
|
|
|
public string CpuName { get; private set; }
|
2017-08-14 17:44:16 +02:00
|
|
|
|
public bool HasBattery { get; private set; }
|
2022-07-29 13:49:26 +02:00
|
|
|
|
public string MacAddress { get; private set; }
|
2019-12-20 17:06:28 +01:00
|
|
|
|
public string Manufacturer { get; private set; }
|
|
|
|
|
public string Model { get; private set; }
|
|
|
|
|
public string Name { get; private set; }
|
2017-08-14 17:44:16 +02:00
|
|
|
|
public OperatingSystem OperatingSystem { get; private set; }
|
2022-07-29 13:49:26 +02:00
|
|
|
|
public string OperatingSystemInfo => $"{OperatingSystemName()}, {Environment.OSVersion.VersionString} ({Architecture()})";
|
2020-05-07 13:21:57 +02:00
|
|
|
|
public string[] PlugAndPlayDeviceIds { get; private set; }
|
2017-08-14 17:44:16 +02:00
|
|
|
|
|
2023-09-01 12:28:03 +02:00
|
|
|
|
public SystemInfo(IRegistry registry)
|
2017-08-14 17:44:16 +02:00
|
|
|
|
{
|
2023-09-01 12:28:03 +02:00
|
|
|
|
this.registry = registry;
|
|
|
|
|
|
2017-08-14 17:44:16 +02:00
|
|
|
|
InitializeBattery();
|
2022-07-29 13:49:26 +02:00
|
|
|
|
InitializeBiosInfo();
|
2023-07-31 10:52:40 +02:00
|
|
|
|
InitializeCpuName();
|
2022-07-29 13:49:26 +02:00
|
|
|
|
InitializeMacAddress();
|
2019-12-20 17:06:28 +01:00
|
|
|
|
InitializeMachineInfo();
|
2017-08-14 17:44:16 +02:00
|
|
|
|
InitializeOperatingSystem();
|
2020-05-06 21:45:04 +02:00
|
|
|
|
InitializePnPDevices();
|
2017-08-14 17:44:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-01 12:28:03 +02:00
|
|
|
|
public IEnumerable<DriveInfo> GetDrives()
|
|
|
|
|
{
|
|
|
|
|
var drives = DriveInfo.GetDrives();
|
|
|
|
|
|
|
|
|
|
registry.TryRead(RegistryValue.UserHive.NoDrives_Key, RegistryValue.UserHive.NoDrives_Name, out var value);
|
|
|
|
|
|
|
|
|
|
if (value is int noDrives && noDrives > 0)
|
|
|
|
|
{
|
|
|
|
|
drives = drives.Where(drive => (noDrives & (int) Math.Pow(2, drive.RootDirectory.ToString()[0] - 65)) == 0).ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return drives;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-14 17:44:16 +02:00
|
|
|
|
private void InitializeBattery()
|
|
|
|
|
{
|
|
|
|
|
var status = SystemInformation.PowerStatus.BatteryChargeStatus;
|
|
|
|
|
|
2022-07-29 13:49:26 +02:00
|
|
|
|
HasBattery = !status.HasFlag(BatteryChargeStatus.NoSystemBattery);
|
|
|
|
|
HasBattery &= !status.HasFlag(BatteryChargeStatus.Unknown);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InitializeBiosInfo()
|
|
|
|
|
{
|
|
|
|
|
var manufacturer = default(string);
|
|
|
|
|
var name = default(string);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_BIOS"))
|
|
|
|
|
using (var results = searcher.Get())
|
|
|
|
|
using (var bios = results.Cast<ManagementObject>().First())
|
|
|
|
|
{
|
|
|
|
|
foreach (var property in bios.Properties)
|
|
|
|
|
{
|
|
|
|
|
if (property.Name.Equals("Manufacturer"))
|
|
|
|
|
{
|
|
|
|
|
manufacturer = Convert.ToString(property.Value);
|
|
|
|
|
}
|
|
|
|
|
else if (property.Name.Equals("Name"))
|
|
|
|
|
{
|
|
|
|
|
name = Convert.ToString(property.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BiosInfo = $"{manufacturer} {name}";
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
BiosInfo = "";
|
|
|
|
|
}
|
2017-08-14 17:44:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-31 10:52:40 +02:00
|
|
|
|
private void InitializeCpuName()
|
2023-07-22 14:19:42 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-07-31 10:52:40 +02:00
|
|
|
|
using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor"))
|
|
|
|
|
using (var results = searcher.Get())
|
2023-07-22 14:19:42 +02:00
|
|
|
|
{
|
2023-07-31 10:52:40 +02:00
|
|
|
|
foreach (var cpu in results)
|
|
|
|
|
{
|
|
|
|
|
using (cpu)
|
|
|
|
|
{
|
|
|
|
|
foreach (var property in cpu.Properties)
|
|
|
|
|
{
|
|
|
|
|
if (property.Name.Equals("Name"))
|
|
|
|
|
{
|
|
|
|
|
CpuName = Convert.ToString(property.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-22 14:19:42 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
2023-07-31 10:52:40 +02:00
|
|
|
|
CpuName = "";
|
2023-07-22 14:19:42 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 17:06:28 +01:00
|
|
|
|
private void InitializeMachineInfo()
|
|
|
|
|
{
|
2020-01-06 15:11:57 +01:00
|
|
|
|
var model = default(string);
|
|
|
|
|
var systemFamily = default(string);
|
2019-12-20 17:52:38 +01:00
|
|
|
|
|
2020-12-04 13:44:40 +01:00
|
|
|
|
try
|
2019-12-20 17:06:28 +01:00
|
|
|
|
{
|
2021-05-30 20:04:44 +02:00
|
|
|
|
using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_ComputerSystem"))
|
2020-12-04 13:44:40 +01:00
|
|
|
|
using (var results = searcher.Get())
|
|
|
|
|
using (var system = results.Cast<ManagementObject>().First())
|
2019-12-20 17:52:38 +01:00
|
|
|
|
{
|
2020-12-04 13:44:40 +01:00
|
|
|
|
foreach (var property in system.Properties)
|
2019-12-20 17:52:38 +01:00
|
|
|
|
{
|
2020-12-04 13:44:40 +01:00
|
|
|
|
if (property.Name.Equals("Manufacturer"))
|
|
|
|
|
{
|
|
|
|
|
Manufacturer = Convert.ToString(property.Value);
|
|
|
|
|
}
|
|
|
|
|
else if (property.Name.Equals("Model"))
|
|
|
|
|
{
|
|
|
|
|
model = Convert.ToString(property.Value);
|
|
|
|
|
}
|
|
|
|
|
else if (property.Name.Equals("Name"))
|
|
|
|
|
{
|
|
|
|
|
Name = Convert.ToString(property.Value);
|
|
|
|
|
}
|
|
|
|
|
else if (property.Name.Equals("SystemFamily"))
|
|
|
|
|
{
|
|
|
|
|
systemFamily = Convert.ToString(property.Value);
|
|
|
|
|
}
|
2019-12-20 17:52:38 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-29 13:49:26 +02:00
|
|
|
|
Model = $"{systemFamily} {model}";
|
2020-12-04 13:44:40 +01:00
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
Manufacturer = "";
|
|
|
|
|
Model = "";
|
|
|
|
|
Name = "";
|
2019-12-20 17:06:28 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-14 17:44:16 +02:00
|
|
|
|
private void InitializeOperatingSystem()
|
|
|
|
|
{
|
2017-08-15 15:30:31 +02:00
|
|
|
|
// IMPORTANT:
|
2022-07-29 13:49:26 +02:00
|
|
|
|
// In order to be able to retrieve the correct operating system version via System.Environment.OSVersion,
|
|
|
|
|
// the executing assembly needs to define an application manifest specifying all supported Windows versions!
|
2019-12-20 17:06:28 +01:00
|
|
|
|
var major = Environment.OSVersion.Version.Major;
|
|
|
|
|
var minor = Environment.OSVersion.Version.Minor;
|
2022-03-28 21:04:31 +02:00
|
|
|
|
var build = Environment.OSVersion.Version.Build;
|
2017-08-14 17:44:16 +02:00
|
|
|
|
|
2017-08-15 15:30:31 +02:00
|
|
|
|
// See https://en.wikipedia.org/wiki/List_of_Microsoft_Windows_versions for mapping source...
|
2017-08-14 17:44:16 +02:00
|
|
|
|
if (major == 6)
|
|
|
|
|
{
|
|
|
|
|
if (minor == 1)
|
|
|
|
|
{
|
|
|
|
|
OperatingSystem = OperatingSystem.Windows7;
|
|
|
|
|
}
|
|
|
|
|
else if (minor == 2)
|
|
|
|
|
{
|
|
|
|
|
OperatingSystem = OperatingSystem.Windows8;
|
|
|
|
|
}
|
|
|
|
|
else if (minor == 3)
|
|
|
|
|
{
|
|
|
|
|
OperatingSystem = OperatingSystem.Windows8_1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (major == 10)
|
|
|
|
|
{
|
2022-03-28 21:04:31 +02:00
|
|
|
|
if (build < 22000)
|
|
|
|
|
{
|
|
|
|
|
OperatingSystem = OperatingSystem.Windows10;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
OperatingSystem = OperatingSystem.Windows11;
|
|
|
|
|
}
|
2017-08-14 17:44:16 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string OperatingSystemName()
|
|
|
|
|
{
|
|
|
|
|
switch (OperatingSystem)
|
|
|
|
|
{
|
|
|
|
|
case OperatingSystem.Windows7:
|
|
|
|
|
return "Windows 7";
|
|
|
|
|
case OperatingSystem.Windows8:
|
|
|
|
|
return "Windows 8";
|
|
|
|
|
case OperatingSystem.Windows8_1:
|
|
|
|
|
return "Windows 8.1";
|
|
|
|
|
case OperatingSystem.Windows10:
|
|
|
|
|
return "Windows 10";
|
2022-03-28 21:04:31 +02:00
|
|
|
|
case OperatingSystem.Windows11:
|
|
|
|
|
return "Windows 11";
|
2017-08-14 17:44:16 +02:00
|
|
|
|
default:
|
|
|
|
|
return "Unknown Windows Version";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string Architecture()
|
|
|
|
|
{
|
2019-12-20 17:06:28 +01:00
|
|
|
|
return Environment.Is64BitOperatingSystem ? "x64" : "x86";
|
2017-08-14 17:44:16 +02:00
|
|
|
|
}
|
2020-05-07 13:21:57 +02:00
|
|
|
|
|
2020-05-06 18:44:08 +02:00
|
|
|
|
private void InitializeMacAddress()
|
|
|
|
|
{
|
2022-07-29 13:49:26 +02:00
|
|
|
|
const string UNDEFINED = "000000000000";
|
|
|
|
|
|
|
|
|
|
try
|
2020-05-06 18:44:08 +02:00
|
|
|
|
{
|
2023-07-31 15:19:31 +02:00
|
|
|
|
using (var searcher = new ManagementObjectSearcher("SELECT MACAddress FROM Win32_NetworkAdapterConfiguration WHERE DNSHostName IS NOT NULL"))
|
2022-07-29 13:49:26 +02:00
|
|
|
|
using (var results = searcher.Get())
|
2023-07-31 15:19:31 +02:00
|
|
|
|
using (var networkAdapter = results.Cast<ManagementObject>().First())
|
2020-05-06 18:44:08 +02:00
|
|
|
|
{
|
2023-07-31 15:19:31 +02:00
|
|
|
|
foreach (var property in networkAdapter.Properties)
|
2020-05-06 18:44:08 +02:00
|
|
|
|
{
|
2023-07-31 15:19:31 +02:00
|
|
|
|
if (property.Name.Equals("MACAddress"))
|
2020-05-06 18:44:08 +02:00
|
|
|
|
{
|
2023-07-31 15:19:31 +02:00
|
|
|
|
MacAddress = Convert.ToString(property.Value).Replace(":", "").ToUpper();
|
2020-05-06 18:44:08 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-29 13:49:26 +02:00
|
|
|
|
}
|
2023-07-31 15:19:31 +02:00
|
|
|
|
finally
|
2022-07-29 13:49:26 +02:00
|
|
|
|
{
|
2023-07-31 15:19:31 +02:00
|
|
|
|
MacAddress = MacAddress ?? UNDEFINED;
|
2020-05-06 18:44:08 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-07 13:21:57 +02:00
|
|
|
|
|
2020-05-06 21:45:04 +02:00
|
|
|
|
private void InitializePnPDevices()
|
|
|
|
|
{
|
2020-05-07 13:21:57 +02:00
|
|
|
|
var deviceList = new List<string>();
|
2020-09-29 14:37:54 +02:00
|
|
|
|
|
2022-07-29 13:49:26 +02:00
|
|
|
|
try
|
2020-05-06 21:45:04 +02:00
|
|
|
|
{
|
2022-07-29 13:49:26 +02:00
|
|
|
|
using (var searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT DeviceID FROM Win32_PnPEntity"))
|
|
|
|
|
using (var results = searcher.Get())
|
2020-05-06 21:45:04 +02:00
|
|
|
|
{
|
2023-07-31 10:52:40 +02:00
|
|
|
|
foreach (var device in results.Cast<ManagementObject>())
|
2022-03-28 21:04:31 +02:00
|
|
|
|
{
|
2023-07-31 10:52:40 +02:00
|
|
|
|
using (device)
|
2020-05-06 21:45:04 +02:00
|
|
|
|
{
|
2023-07-31 10:52:40 +02:00
|
|
|
|
foreach (var property in device.Properties)
|
2020-05-06 21:45:04 +02:00
|
|
|
|
{
|
2022-07-29 13:49:26 +02:00
|
|
|
|
if (property.Name.Equals("DeviceID"))
|
|
|
|
|
{
|
|
|
|
|
deviceList.Add(Convert.ToString(property.Value).ToLower());
|
|
|
|
|
}
|
2020-05-06 21:45:04 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-07 13:21:57 +02:00
|
|
|
|
}
|
2020-05-06 21:45:04 +02:00
|
|
|
|
}
|
2022-07-29 13:49:26 +02:00
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
2020-09-29 14:37:54 +02:00
|
|
|
|
PlugAndPlayDeviceIds = deviceList.ToArray();
|
2020-05-06 21:45:04 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-14 17:44:16 +02:00
|
|
|
|
}
|
|
|
|
|
}
|