389 lines
14 KiB
C#
389 lines
14 KiB
C#
using NLog;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Management;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace EgwCoreLib.Utils
|
|
{
|
|
/// <summary>
|
|
/// Gestione info macchina
|
|
/// ATTENZIONE! per ora solo windows...
|
|
/// </summary>
|
|
public class MachineInfo
|
|
{
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// Restitusice info richieste
|
|
/// </summary>
|
|
/// <param name="infoType"></param>
|
|
/// <returns></returns>
|
|
public static Dictionary<string, string> GetInfo(string infoType)
|
|
{
|
|
var outInfo = new Dictionary<string, string>();
|
|
switch (infoType)
|
|
{
|
|
case "BIOS":
|
|
outInfo = GetBiosInfo();
|
|
break;
|
|
|
|
case "CPU":
|
|
outInfo = GetCpuInfo();
|
|
break;
|
|
|
|
case "NET":
|
|
outInfo = GetNetInfo();
|
|
break;
|
|
|
|
case "OS":
|
|
outInfo = GetOsInfo();
|
|
break;
|
|
|
|
case "RAM":
|
|
outInfo = GetRamInfo();
|
|
break;
|
|
|
|
case "USER":
|
|
outInfo = GetUserInfo();
|
|
break;
|
|
|
|
case "VOL":
|
|
outInfo = GetVolumeInfo();
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
return outInfo;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Private Fields
|
|
|
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Methods
|
|
|
|
/// <summary>
|
|
/// Info BIOS
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private static Dictionary<string, string> GetBiosInfo()
|
|
{
|
|
ManagementObjectCollection moc;
|
|
Dictionary<string, string> DictParam = new Dictionary<string, string>();
|
|
|
|
try
|
|
{
|
|
moc = new ManagementObjectSearcher("select * from Win32_BIOS").Get();
|
|
}
|
|
catch
|
|
{
|
|
Log.Error("Error: WMI API Not loaded.");
|
|
return DictParam;
|
|
}
|
|
|
|
foreach (ManagementObject obj in moc)
|
|
{
|
|
searchWmiParam(DictParam, obj, "Manufacturer", "Manufacturer");
|
|
searchWmiParam(DictParam, obj, "ReleaseDate", "ReleaseDate");
|
|
searchWmiParam(DictParam, obj, "SMBIOSBIOSVersion", "SMBIOSBIOSVersion");
|
|
searchWmiParam(DictParam, obj, "SMBIOSMajorVersion", "SMBIOSMajorVersion");
|
|
searchWmiParam(DictParam, obj, "SMBIOSMinorVersion", "SMBIOSMinorVersion");
|
|
searchWmiParam(DictParam, obj, "SoftwareElementID", "SoftwareElementID");
|
|
searchWmiParam(DictParam, obj, "SoftwareElementState", "SoftwareElementState");
|
|
searchWmiParam(DictParam, obj, "SystemBiosMajorVersion", "SystemBiosMajorVersion");
|
|
searchWmiParam(DictParam, obj, "SystemBiosMajorVersion", "SystemBiosMajorVersion");
|
|
searchWmiParam(DictParam, obj, "SystemBiosMinorVersion", "SystemBiosMinorVersion");
|
|
searchWmiParam(DictParam, obj, "BiosVersion", "Version");
|
|
}
|
|
return DictParam;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Info processore
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private static Dictionary<string, string> GetCpuInfo()
|
|
{
|
|
ManagementObjectCollection moc;
|
|
Dictionary<string, string> DictParam = new Dictionary<string, string>();
|
|
|
|
try
|
|
{
|
|
moc = new ManagementObjectSearcher("select * from Win32_Processor").Get();
|
|
}
|
|
catch
|
|
{
|
|
Log.Error("Error: WMI API Not loaded.");
|
|
return DictParam;
|
|
}
|
|
|
|
foreach (ManagementObject obj in moc)
|
|
{
|
|
searchWmiParam(DictParam, obj, "AddressWidth", "AddressWidth");
|
|
searchWmiParam(DictParam, obj, "Architecture", "Architecture");
|
|
searchWmiParam(DictParam, obj, "CurrentClockSpeed", "CurrentClockSpeed");
|
|
searchWmiParam(DictParam, obj, "CurrentVoltage", "CurrentVoltage");
|
|
searchWmiParam(DictParam, obj, "DataWidth", "DataWidth");
|
|
searchWmiParam(DictParam, obj, "Description", "Description");
|
|
searchWmiParam(DictParam, obj, "ExtClock", "ExtClock");
|
|
searchWmiParam(DictParam, obj, "Family", "Family");
|
|
searchWmiParam(DictParam, obj, "L2CacheSize", "L2CacheSize");
|
|
searchWmiParam(DictParam, obj, "L3CacheSize", "L3CacheSize");
|
|
searchWmiParam(DictParam, obj, "Level", "Level");
|
|
searchWmiParam(DictParam, obj, "LoadPercentage", "LoadPercentage");
|
|
searchWmiParam(DictParam, obj, "Manufacturer", "Manufacturer");
|
|
searchWmiParam(DictParam, obj, "NumberOfCores", "NumberOfCores");
|
|
searchWmiParam(DictParam, obj, "NumberOfLogicalProcessors", "NumberOfLogicalProcessors");
|
|
searchWmiParam(DictParam, obj, "ProcessorId", "ProcessorId");
|
|
searchWmiParam(DictParam, obj, "ProcessorType", "ProcessorType");
|
|
searchWmiParam(DictParam, obj, "Revision", "Revision");
|
|
searchWmiParam(DictParam, obj, "Role", "Role");
|
|
searchWmiParam(DictParam, obj, "SocketDesignation", "SocketDesignation");
|
|
searchWmiParam(DictParam, obj, "SystemName", "SystemName");
|
|
searchWmiParam(DictParam, obj, "CpuVersion", "Version");
|
|
}
|
|
return DictParam;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Info Networking
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private static Dictionary<string, string> GetNetInfo()
|
|
{
|
|
ManagementObjectCollection moc;
|
|
Dictionary<string, string> DictParam = new Dictionary<string, string>();
|
|
|
|
try
|
|
{
|
|
moc = new ManagementObjectSearcher("select * from Win32_NetworkAdapter WHERE NetEnabled = 'true'").Get();
|
|
}
|
|
catch
|
|
{
|
|
Log.Error("Error: WMI API Not loaded.");
|
|
return DictParam;
|
|
}
|
|
|
|
foreach (ManagementObject obj in moc)
|
|
{
|
|
searchWmiParam(DictParam, obj, "AdapterType", "AdapterType");
|
|
searchWmiParam(DictParam, obj, "Description", "Description");
|
|
searchWmiParam(DictParam, obj, "GUID", "GUID");
|
|
searchWmiParam(DictParam, obj, "MACAddress", "MACAddress");
|
|
searchWmiParam(DictParam, obj, "Manufacturer", "Manufacturer");
|
|
searchWmiParam(DictParam, obj, "Name", "Name");
|
|
searchWmiParam(DictParam, obj, "SystemName", "SystemName");
|
|
searchWmiParam(DictParam, obj, "PhysicalAdapter", "PhysicalAdapter");
|
|
searchWmiParam(DictParam, obj, "ProductName", "ProductName");
|
|
searchWmiParam(DictParam, obj, "ServiceName", "ServiceName");
|
|
}
|
|
|
|
// ora cerco IP...
|
|
try
|
|
{
|
|
moc = new ManagementObjectSearcher("select * from Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'true'").Get();
|
|
}
|
|
catch
|
|
{
|
|
Log.Error("Error: WMI API Not loaded.");
|
|
return DictParam;
|
|
}
|
|
int idx = 0;
|
|
foreach (ManagementObject obj in moc)
|
|
{
|
|
searchWmiParam(DictParam, obj, $"Int.{idx}.Name", "Caption");
|
|
searchWmiParamMulti(DictParam, obj, $"Int.{idx}.Ipv4", "IPAddress", ".");
|
|
searchWmiParamMulti(DictParam, obj, $"Int.{idx}.Ipv6", "IPAddress", ":");
|
|
idx++;
|
|
}
|
|
return DictParam;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Info OS
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private static Dictionary<string, string> GetOsInfo()
|
|
{
|
|
ManagementObjectCollection moc;
|
|
Dictionary<string, string> DictParam = new Dictionary<string, string>();
|
|
|
|
try
|
|
{
|
|
moc = new ManagementObjectSearcher("select * from Win32_OperatingSystem").Get();
|
|
}
|
|
catch
|
|
{
|
|
Log.Error("Error: WMI API Not loaded.");
|
|
return DictParam;
|
|
}
|
|
|
|
foreach (ManagementObject obj in moc)
|
|
{
|
|
searchWmiParam(DictParam, obj, "Caption", "Caption");
|
|
searchWmiParam(DictParam, obj, "Version", "Version");
|
|
searchWmiParam(DictParam, obj, "MaxNumberOfProcesses", "MaxNumberOfProcesses");
|
|
searchWmiParam(DictParam, obj, "MaxProcessMemorySize", "MaxProcessMemorySize");
|
|
searchWmiParam(DictParam, obj, "OSArchitecture", "OSArchitecture");
|
|
searchWmiParam(DictParam, obj, "SerialNumber", "SerialNumber");
|
|
searchWmiParam(DictParam, obj, "BuildNumber", "BuildNumber");
|
|
searchWmiParam(DictParam, obj, "RegisteredUser", "RegisteredUser");
|
|
}
|
|
return DictParam;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Info RAM
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private static Dictionary<string, string> GetRamInfo()
|
|
{
|
|
ManagementObjectCollection moc;
|
|
Dictionary<string, string> DictParam = new Dictionary<string, string>();
|
|
|
|
try
|
|
{
|
|
moc = new ManagementObjectSearcher("select * from Win32_PhysicalMemory").Get();
|
|
}
|
|
catch
|
|
{
|
|
Log.Error("Error: WMI API Not loaded.");
|
|
return DictParam;
|
|
}
|
|
|
|
foreach (ManagementObject obj in moc)
|
|
{
|
|
searchWmiParam(DictParam, obj, "DeviceLocator", "DeviceLocator");
|
|
searchWmiParam(DictParam, obj, "DataWidth", "DataWidth");
|
|
searchWmiParam(DictParam, obj, "ConfiguredVoltage", "ConfiguredVoltage");
|
|
searchWmiParam(DictParam, obj, "FormFactor", "FormFactor");
|
|
searchWmiParam(DictParam, obj, "ConfiguredClockSpeed", "ConfiguredClockSpeed");
|
|
searchWmiParam(DictParam, obj, "MaxVoltage", "MaxVoltage");
|
|
searchWmiParam(DictParam, obj, "MinVoltage", "MinVoltage");
|
|
searchWmiParam(DictParam, obj, "Speed", "Speed");
|
|
searchWmiParam(DictParam, obj, "SMBIOSMemoryType", "SMBIOSMemoryType");
|
|
searchWmiParam(DictParam, obj, "PartNumber", "PartNumber");
|
|
}
|
|
return DictParam;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Info User (macchina, dominio, username)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private static Dictionary<string, string> GetUserInfo()
|
|
{
|
|
ManagementObjectCollection moc;
|
|
Dictionary<string, string> DictParam = new Dictionary<string, string>();
|
|
try
|
|
{
|
|
DictParam.Add("MachineName", Environment.MachineName);
|
|
DictParam.Add("UserDomainName", Environment.UserDomainName);
|
|
DictParam.Add("UserName", Environment.UserName);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error in GetUserInfo{Environment.NewLine}{exc}");
|
|
}
|
|
|
|
return DictParam;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Info Volumes (HDD/SSD)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private static Dictionary<string, string> GetVolumeInfo()
|
|
{
|
|
ManagementObjectCollection moc;
|
|
Dictionary<string, string> DictParam = new Dictionary<string, string>();
|
|
|
|
try
|
|
{
|
|
moc = new ManagementObjectSearcher("select DriveLetter, DeviceID from Win32_Volume").Get();
|
|
}
|
|
catch
|
|
{
|
|
Log.Error("Error: WMI API Not loaded.");
|
|
return DictParam;
|
|
}
|
|
|
|
foreach (ManagementObject obj in moc)
|
|
{
|
|
// mi limito al disco "C:"
|
|
string sDrive = "DriveLetter";
|
|
var objVal = $"{obj[sDrive]}";
|
|
if (objVal == "C:")
|
|
{
|
|
searchWmiParam(DictParam, obj, "DeviceID", "DeviceID");
|
|
searchWmiParam(DictParam, obj, "DriveLetter", "DriveLetter");
|
|
}
|
|
}
|
|
return DictParam;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua estrazione parametri WMI da elenco recuperato
|
|
/// </summary>
|
|
/// <param name="OutDict"></param>
|
|
/// <param name="obj"></param>
|
|
/// <param name="pName"></param>
|
|
/// <param name="pKey"></param>
|
|
private static void searchWmiParam(Dictionary<string, string> OutDict, ManagementObject obj, string pName, string pKey)
|
|
{
|
|
try
|
|
{
|
|
var objVal = $"{obj[pKey]}";
|
|
string strVal = objVal != null ? objVal : "";
|
|
OutDict.Add(pName, strVal);
|
|
}
|
|
catch
|
|
{ }
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua estrazione parametri WMI da elenco recuperato come array
|
|
/// </summary>
|
|
/// <param name="OutDict"></param>
|
|
/// <param name="obj"></param>
|
|
/// <param name="pName"></param>
|
|
/// <param name="pKey"></param>
|
|
/// <param name="filtCond"></param>
|
|
private static void searchWmiParamMulti(Dictionary<string, string> OutDict, ManagementObject obj, string pName, string pKey, string filtCond)
|
|
{
|
|
try
|
|
{
|
|
int id = 0;
|
|
var objVal = (string[])obj[pKey];
|
|
var filtObj = objVal.Where(x => x.Contains(filtCond)).ToList();
|
|
bool hasMulti = filtObj.Count > 1;
|
|
foreach (var str in filtObj)
|
|
{
|
|
if (hasMulti)
|
|
{
|
|
OutDict.Add($"{pName}.{id++}", str);
|
|
}
|
|
else
|
|
{
|
|
OutDict.Add($"{pName}", str);
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{ }
|
|
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
}
|