<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;

namespace Disco.Client.Interop
{
    public static class SystemAudit
    {

        public static string DeviceSerialNumber { get; private set; }
        public static string DeviceSMBIOSVersion { get; private set; }
        public static string DeviceManufacturer { get; private set; }
        public static string DeviceModel { get; private set; }
        public static string DeviceType { get; private set; }
        public static string DeviceUUID { get; private set; }
        public static bool DeviceIsPartOfDomain { get; private set; }

        static SystemAudit()
        {
            // Get BIOS Information
            try
            {
                using (ManagementObjectSearcher mSearcher = new ManagementObjectSearcher("SELECT SerialNumber, SMBIOSBIOSVersion FROM Win32_BIOS WHERE PrimaryBios=true"))
                {
                    using (ManagementObjectCollection mResults = mSearcher.Get())
                    {
                        using (var mItem = mResults.Cast<ManagementObject>().FirstOrDefault())
                        {
                            if (mItem != null)
                            {
                                DeviceSerialNumber = mItem.GetPropertyValue("SerialNumber").ToString().Trim();
                                ErrorReporting.DeviceIdentifier = DeviceSerialNumber;
                                DeviceSMBIOSVersion = mItem.GetPropertyValue("SMBIOSBIOSVersion").ToString().Trim();
                            }
                            else
                            {
                                throw new Exception("No Win32_BIOS WHERE PrimaryBios=true was found");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Disco Client was unable to retrieve BIOS information from WMI", ex);
            }

            // Get System Information
            try
            {
                using (ManagementObjectSearcher mSearcher = new ManagementObjectSearcher("SELECT Manufacturer, Model, PartOfDomain, PCSystemType FROM Win32_ComputerSystem"))
                {
                    using (ManagementObjectCollection mResults = mSearcher.Get())
                    {
                        using (var mItem = mResults.Cast<ManagementObject>().FirstOrDefault())
                        {
                            if (mItem != null)
                            {
                                DeviceManufacturer = mItem.GetPropertyValue("Manufacturer").ToString().Trim();
                                DeviceModel = mItem.GetPropertyValue("Model").ToString().Trim();
                                DeviceIsPartOfDomain = bool.Parse(mItem.GetPropertyValue("PartOfDomain").ToString());
                                DeviceType = PCSystemTypeToString((UInt16)mItem.GetPropertyValue("PCSystemType"));
                            }
                            else
                            {
                                throw new Exception("No Win32_ComputerSystem was found");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Disco Client was unable to retrieve ComputerSystem information from WMI", ex);
            }

            // Get System Product Information
            string ComputerSystemProductSerialNumber;
            try
            {
                using (ManagementObjectSearcher mSearcher = new ManagementObjectSearcher("SELECT IdentifyingNumber, UUID FROM Win32_ComputerSystemProduct"))
                {
                    using (ManagementObjectCollection mResults = mSearcher.Get())
                    {
                        using (var mItem = mResults.Cast<ManagementObject>().FirstOrDefault())
                        {
                            if (mItem != null)
                            {
                                ComputerSystemProductSerialNumber = mItem.GetPropertyValue("IdentifyingNumber").ToString().Trim();
                                DeviceUUID = mItem.GetPropertyValue("UUID").ToString().Trim();
                            }
                            else
                            {
                                throw new Exception("No Win32_ComputerSystemProduct was found");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Disco Client was unable to retrieve ComputerSystemProduct information from WMI", ex);
            }

            // Added 2012-11-22 G# - Lenovo IdeaPad Serial SHIM
            // http://www.discoict.com.au/forum/feature-requests/2012/11/serial-number-detection-on-ideapads.aspx
            if (string.IsNullOrWhiteSpace(DeviceSerialNumber) ||
                (DeviceManufacturer.Equals("LENOVO", StringComparison.InvariantCultureIgnoreCase) &&
                (DeviceModel.Equals("S10-3", StringComparison.InvariantCultureIgnoreCase) // S10-3
                || DeviceModel.Equals("2957", StringComparison.InvariantCultureIgnoreCase)))) // S10-2
            {
                try
                {
                    using (ManagementObjectSearcher mSearcher = new ManagementObjectSearcher("SELECT SerialNumber FROM Win32_BaseBoard"))
                    {
                        using (ManagementObjectCollection mResults = mSearcher.Get())
                        {
                            using (var mItem = mResults.Cast<ManagementObject>().FirstOrDefault())
                            {
                                if (mItem != null)
                                {
                                    DeviceSerialNumber = mItem.GetPropertyValue("SerialNumber").ToString().Trim();
                                }
                                else
                                {
                                    throw new Exception("No Win32_BaseBoard was found");
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("Disco Client was unable to retrieve BaseBoard information from WMI", ex);
                }
                if (string.IsNullOrWhiteSpace(DeviceSerialNumber))
                    DeviceSerialNumber = ComputerSystemProductSerialNumber;
            }
            // End Added 2012-11-22 G#

            ErrorReporting.DeviceIdentifier = DeviceSerialNumber;<commit_msg>Fix: Client WMI Serial Number<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;

namespace Disco.Client.Interop
{
    public static class SystemAudit
    {

        public static string DeviceSerialNumber { get; private set; }
        public static string DeviceSMBIOSVersion { get; private set; }
        public static string DeviceManufacturer { get; private set; }
        public static string DeviceModel { get; private set; }
        public static string DeviceType { get; private set; }
        public static string DeviceUUID { get; private set; }
        public static bool DeviceIsPartOfDomain { get; private set; }

        static SystemAudit()
        {
            // Get BIOS Information
            try
            {
                using (ManagementObjectSearcher mSearcher = new ManagementObjectSearcher("SELECT SerialNumber, SMBIOSBIOSVersion FROM Win32_BIOS WHERE PrimaryBios=true"))
                {
                    using (ManagementObjectCollection mResults = mSearcher.Get())
                    {
                        using (var mItem = mResults.Cast<ManagementObject>().FirstOrDefault())
                        {
                            if (mItem != null)
                            {
                                DeviceSerialNumber = mItem.GetPropertyValue("SerialNumber") as string;
                                if (!string.IsNullOrEmpty(DeviceSerialNumber))
                                    DeviceSerialNumber = DeviceSerialNumber.Trim();

                                ErrorReporting.DeviceIdentifier = DeviceSerialNumber;
                                DeviceSMBIOSVersion = mItem.GetPropertyValue("SMBIOSBIOSVersion") as string;
                                if (!string.IsNullOrEmpty(DeviceSMBIOSVersion))
                                    DeviceSMBIOSVersion = DeviceSMBIOSVersion.Trim();
                            }
                            else
                            {
                                throw new Exception("No Win32_BIOS WHERE PrimaryBios=true was found");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Disco Client was unable to retrieve BIOS information from WMI", ex);
            }

            // Get System Information
            try
            {
                using (ManagementObjectSearcher mSearcher = new ManagementObjectSearcher("SELECT Manufacturer, Model, PartOfDomain, PCSystemType FROM Win32_ComputerSystem"))
                {
                    using (ManagementObjectCollection mResults = mSearcher.Get())
                    {
                        using (var mItem = mResults.Cast<ManagementObject>().FirstOrDefault())
                        {
                            if (mItem != null)
                            {
                                DeviceManufacturer = mItem.GetPropertyValue("Manufacturer") as string;
                                if (!string.IsNullOrEmpty(DeviceManufacturer))
                                    DeviceManufacturer = DeviceManufacturer.Trim();

                                DeviceModel = mItem.GetPropertyValue("Model") as string;
                                if (!string.IsNullOrEmpty(DeviceModel))
                                    DeviceModel = DeviceModel.Trim();

                                DeviceIsPartOfDomain = (bool)mItem.GetPropertyValue("PartOfDomain");
                                DeviceType = PCSystemTypeToString((UInt16)mItem.GetPropertyValue("PCSystemType"));
                            }
                            else
                            {
                                throw new Exception("No Win32_ComputerSystem was found");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Disco Client was unable to retrieve ComputerSystem information from WMI", ex);
            }

            // Get System Product Information
            string ComputerSystemProductSerialNumber;
            try
            {
                using (ManagementObjectSearcher mSearcher = new ManagementObjectSearcher("SELECT IdentifyingNumber, UUID FROM Win32_ComputerSystemProduct"))
                {
                    using (ManagementObjectCollection mResults = mSearcher.Get())
                    {
                        using (var mItem = mResults.Cast<ManagementObject>().FirstOrDefault())
                        {
                            if (mItem != null)
                            {
                                ComputerSystemProductSerialNumber = mItem.GetPropertyValue("IdentifyingNumber") as string;
                                if (!string.IsNullOrEmpty(ComputerSystemProductSerialNumber))
                                    ComputerSystemProductSerialNumber = ComputerSystemProductSerialNumber.Trim();

                                DeviceUUID = mItem.GetPropertyValue("UUID") as string;
                                if (!string.IsNullOrEmpty(DeviceUUID))
                                    DeviceUUID = DeviceUUID.Trim();
                            }
                            else
                            {
                                throw new Exception("No Win32_ComputerSystemProduct was found");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Disco Client was unable to retrieve ComputerSystemProduct information from WMI", ex);
            }

            // Added 2012-11-22 G# - Lenovo IdeaPad Serial SHIM
            // http://www.discoict.com.au/forum/feature-requests/2012/11/serial-number-detection-on-ideapads.aspx
            if (string.IsNullOrWhiteSpace(DeviceSerialNumber) ||
                (DeviceManufacturer.Equals("LENOVO", StringComparison.InvariantCultureIgnoreCase) &&
                (DeviceModel.Equals("S10-3", StringComparison.InvariantCultureIgnoreCase) // S10-3
                || DeviceModel.Equals("2957", StringComparison.InvariantCultureIgnoreCase)))) // S10-2
            {
                try
                {
                    using (ManagementObjectSearcher mSearcher = new ManagementObjectSearcher("SELECT SerialNumber FROM Win32_BaseBoard"))
                    {
                        using (ManagementObjectCollection mResults = mSearcher.Get())
                        {
                            using (var mItem = mResults.Cast<ManagementObject>().FirstOrDefault())
                            {
                                if (mItem != null)
                                {
                                    DeviceSerialNumber = mItem.GetPropertyValue("SerialNumber") as string;
                                    if (!string.IsNullOrEmpty(DeviceSerialNumber))
                                        DeviceSerialNumber = DeviceSerialNumber.Trim();
                                }
                                else
                                {
                                    throw new Exception("No Win32_BaseBoard was found");
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("Disco Client was unable to retrieve BaseBoard information from WMI", ex);
                }
                if (string.IsNullOrWhiteSpace(DeviceSerialNumber))
                    DeviceSerialNumber = ComputerSystemProductSerialNumber;
            }
            // End Added 2012-11-22 G#

            ErrorReporting.DeviceIdentifier = DeviceSerialNumber;
{'fraction_non_alphanumeric': 0.0681189721454104, 'fraction_numerical': 0.00714911984892426, 'mean_word_length': 1.1928423543330375, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6627436', 'n_tokens_mistral': 3219, 'n_tokens_neox': 3004, 'n_words': 665}