From ae7dcc0b0bd21ba1c79a5d44ac975de1fcb336c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damian=20B=C3=BCchel?= Date: Tue, 29 Sep 2020 14:37:54 +0200 Subject: [PATCH] Code cleanup. --- SafeExamBrowser.SystemComponents/SystemInfo.cs | 7 +++---- .../VirtualMachineDetector.cs | 12 +++++++----- .../Windows/FileSystemDialog.xaml.cs | 4 ++-- .../Windows/FileSystemDialog.xaml.cs | 4 ++-- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/SafeExamBrowser.SystemComponents/SystemInfo.cs b/SafeExamBrowser.SystemComponents/SystemInfo.cs index 3eb62c72..a91c5db4 100644 --- a/SafeExamBrowser.SystemComponents/SystemInfo.cs +++ b/SafeExamBrowser.SystemComponents/SystemInfo.cs @@ -136,17 +136,15 @@ namespace SafeExamBrowser.SystemComponents private void InitializeMacAddress() { - using (var searcher = new ManagementObjectSearcher("Select MACAddress from Win32_NetworkAdapterConfiguration WHERE DNSDomain IS NOT NULL")) + using (var searcher = new ManagementObjectSearcher("SELECT MACAddress FROM Win32_NetworkAdapterConfiguration WHERE DNSDomain IS NOT NULL")) using (var results = searcher.Get()) { - if (results != null && results.Count > 0) { using (var networkAdapter = results.Cast().First()) { foreach (var property in networkAdapter.Properties) { - if (property.Name.Equals("MACAddress")) { MacAddress = Convert.ToString(property.Value).Replace(":", "").ToUpper(); @@ -164,6 +162,7 @@ namespace SafeExamBrowser.SystemComponents private void InitializePnPDevices() { var deviceList = new List(); + using (var searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT DeviceID FROM Win32_PnPEntity")) using (var results = searcher.Get()) { @@ -180,8 +179,8 @@ namespace SafeExamBrowser.SystemComponents } } } - PlugAndPlayDeviceIds = deviceList.ToArray(); + PlugAndPlayDeviceIds = deviceList.ToArray(); } } } diff --git a/SafeExamBrowser.SystemComponents/VirtualMachineDetector.cs b/SafeExamBrowser.SystemComponents/VirtualMachineDetector.cs index b60fcdbf..98c3e6d1 100644 --- a/SafeExamBrowser.SystemComponents/VirtualMachineDetector.cs +++ b/SafeExamBrowser.SystemComponents/VirtualMachineDetector.cs @@ -6,16 +6,19 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +using System.Linq; using SafeExamBrowser.Logging.Contracts; using SafeExamBrowser.SystemComponents.Contracts; -using System.Globalization; -using System.Linq; namespace SafeExamBrowser.SystemComponents { public class VirtualMachineDetector : IVirtualMachineDetector { - private static readonly string[] PCI_VENDOR_BLACKLIST = { "vbox", "vid_80ee", "qemu", "ven_1af4", "ven_1b36", "subsys_11001af4" }; //Virtualbox: VBOX, 80EE RedHat: QUEMU, 1AF4, 1B36 + /// + /// Virtualbox: VBOX, 80EE + /// RedHat: QUEMU, 1AF4, 1B36 + /// + private static readonly string[] PCI_VENDOR_BLACKLIST = { "vbox", "vid_80ee", "qemu", "ven_1af4", "ven_1b36", "subsys_11001af4" }; private static readonly string VIRTUALBOX_MAC_PREFIX = "080027"; private static readonly string QEMU_MAC_PREFIX = "525400"; @@ -44,13 +47,12 @@ namespace SafeExamBrowser.SystemComponents if (macAddress != null && macAddress.Count() > 2) { - isVirtualMachine |= (macAddress.StartsWith(QEMU_MAC_PREFIX) || macAddress.StartsWith(VIRTUALBOX_MAC_PREFIX)); + isVirtualMachine |= macAddress.StartsWith(QEMU_MAC_PREFIX) || macAddress.StartsWith(VIRTUALBOX_MAC_PREFIX); } foreach (var device in plugAndPlayDeviceIds) { isVirtualMachine |= PCI_VENDOR_BLACKLIST.Any(device.ToLower().Contains); - } logger.Debug($"Computer '{systemInfo.Name}' appears to {(isVirtualMachine ? "" : "not ")}be a virtual machine."); diff --git a/SafeExamBrowser.UserInterface.Desktop/Windows/FileSystemDialog.xaml.cs b/SafeExamBrowser.UserInterface.Desktop/Windows/FileSystemDialog.xaml.cs index a43095b5..f32b9eba 100644 --- a/SafeExamBrowser.UserInterface.Desktop/Windows/FileSystemDialog.xaml.cs +++ b/SafeExamBrowser.UserInterface.Desktop/Windows/FileSystemDialog.xaml.cs @@ -292,11 +292,11 @@ namespace SafeExamBrowser.UserInterface.Desktop.Windows private DriveInfo[] GetDrives() { var drives = DriveInfo.GetDrives(); - int noDrives = (int)Registry.GetValue("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer", "NoDrives", 0); + var noDrives = (int) Registry.GetValue("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer", "NoDrives", 0); if (noDrives > 0) { - return drives.Where(drive => (noDrives & (int)(Math.Pow(2, (int)(drive.RootDirectory.ToString()[0]) - 65))) == 0).ToArray(); + return drives.Where(drive => (noDrives & (int) Math.Pow(2, drive.RootDirectory.ToString()[0] - 65)) == 0).ToArray(); } return drives; diff --git a/SafeExamBrowser.UserInterface.Mobile/Windows/FileSystemDialog.xaml.cs b/SafeExamBrowser.UserInterface.Mobile/Windows/FileSystemDialog.xaml.cs index e039c06e..937406c3 100644 --- a/SafeExamBrowser.UserInterface.Mobile/Windows/FileSystemDialog.xaml.cs +++ b/SafeExamBrowser.UserInterface.Mobile/Windows/FileSystemDialog.xaml.cs @@ -292,11 +292,11 @@ namespace SafeExamBrowser.UserInterface.Mobile.Windows private DriveInfo[] GetDrives() { var drives = DriveInfo.GetDrives(); - int noDrives = (int)Registry.GetValue("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer", "NoDrives", 0); + var noDrives = (int) Registry.GetValue("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer", "NoDrives", 0); if (noDrives > 0) { - return drives.Where(drive => (noDrives & (int)(Math.Pow(2, (int)(drive.RootDirectory.ToString()[0]) - 65))) == 0).ToArray(); + return drives.Where(drive => (noDrives & (int) Math.Pow(2, drive.RootDirectory.ToString()[0] - 65)) == 0).ToArray(); } return drives;