Files
SteamWare/SteamWareLib/Diagnostica.cs
T
2018-03-17 09:47:02 +01:00

291 lines
13 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Ports;
using System.Management;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
namespace SteamWare
{
/// <summary>
/// helper x raccolta dati di diagnostica
/// </summary>
public class Diagnostica
{
/// <summary>
/// uptime macchina (formattato)
/// </summary>
public static string uptime
{
get
{
TimeSpan upt = GetUptime();
return string.Format("{0} gg, {1} h, {2} min, {3} sec", upt.Days, upt.Hours, upt.Minutes, upt.Seconds);
}
}
/// <summary>
/// calcolo uptime in formato timespan
/// </summary>
/// <returns></returns>
public static TimeSpan GetUptime()
{
ManagementObject mo = new ManagementObject(@"\\.\root\cimv2:Win32_OperatingSystem=@");
DateTime lastBootUp = ManagementDateTimeConverter.ToDateTime(mo["LastBootUpTime"].ToString());
return DateTime.Now.ToUniversalTime() - lastBootUp.ToUniversalTime();
}
/// <summary>
/// restituisce elenco dischi con utilizzo (formattato)
/// </summary>
/// <returns></returns>
public static string diskUsage
{
get
{
string answ = "";
DriveInfo[] allDrives = DriveInfo.GetDrives();
// ciclo tutti i drives...
foreach (DriveInfo d in allDrives)
{
answ += SteamwareStrings.addLine(string.Format("Drive {0}", d.Name));
answ += SteamwareStrings.addLine(string.Format(" File type: {0}", d.DriveType));
if (d.IsReady == true)
{
answ += SteamwareStrings.addLine(string.Format(" Volume label: {0}", d.VolumeLabel));
answ += SteamwareStrings.addLine(string.Format(" File system: {0}", d.DriveFormat));
answ += SteamwareStrings.addLine(string.Format(" Available space to current user:{0:#,###} Mb", d.AvailableFreeSpace / 1024 / 1024));
answ += SteamwareStrings.addLine(string.Format(" Total available space: {0:#,###} Mb", d.TotalFreeSpace / 1024 / 1024));
answ += SteamwareStrings.addLine(string.Format(" Total size of drive: {0:#,###} Mb ", d.TotalSize / 1024 / 1024));
}
}
return answ;
}
}
/// <summary>
/// mostra elenco interfacce di rete e loro conf
/// </summary>
public static string networkInterfaces
{
get
{
string answ = "";
IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
answ += SteamwareStrings.addLine(string.Format("Interface information for {0}.{1} ", computerProperties.HostName, computerProperties.DomainName));
if (nics == null || nics.Length < 1)
{
answ += SteamwareStrings.addLine(string.Format(" No network interfaces found."));
}
else
{
answ += SteamwareStrings.addLine(string.Format(" Number of interfaces .................... : {0}", nics.Length));
foreach (NetworkInterface adapter in nics)
{
IPInterfaceProperties properties = adapter.GetIPProperties();
answ += SteamwareStrings.addLine("");
answ += SteamwareStrings.addLine(string.Format(adapter.Description));
answ += SteamwareStrings.addLine(string.Format(String.Empty.PadLeft(adapter.Description.Length, '=')));
answ += SteamwareStrings.addLine(string.Format(" Interface type .......................... : {0}", adapter.NetworkInterfaceType));
answ += SteamwareStrings.addLine(string.Format(" Physical Address ........................ : {0}", adapter.GetPhysicalAddress().ToString()));
answ += SteamwareStrings.addLine(string.Format(" Operational status ...................... : {0}", adapter.OperationalStatus));
string versions = "";
// Create a display string for the supported IP versions.
if (adapter.Supports(NetworkInterfaceComponent.IPv4))
{
versions = "IPv4";
// elenco IPv4
IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
IPAddressCollection addresses = adapterProperties.DhcpServerAddresses;
if (addresses.Count > 0)
{
Console.WriteLine(adapter.Description);
foreach (IPAddress address in addresses)
{
answ += SteamwareStrings.addLine(string.Format(" IPv4 address ............................ : {0}", address));
}
}
}
if (adapter.Supports(NetworkInterfaceComponent.IPv6))
{
if (versions.Length > 0)
{
versions += " ";
}
versions += "IPv6";
}
// The following information is not useful for loopback adapters.
if (adapter.NetworkInterfaceType == NetworkInterfaceType.Loopback)
{
continue;
}
answ += SteamwareStrings.addLine(string.Format(" DNS suffix .............................. : {0}", properties.DnsSuffix));
if (adapter.Supports(NetworkInterfaceComponent.IPv4))
{
IPv4InterfaceProperties ipv4 = properties.GetIPv4Properties();
answ += SteamwareStrings.addLine(string.Format(" MTU...................................... : {0}", ipv4.Mtu));
}
answ += SteamwareStrings.addLine(string.Format(" DNS enabled ............................. : {0}", properties.IsDnsEnabled));
answ += SteamwareStrings.addLine(string.Format(" Dynamically configured DNS .............. : {0}", properties.IsDynamicDnsEnabled));
answ += SteamwareStrings.addLine(string.Format(" Receive Only ............................ : {0}", adapter.IsReceiveOnly));
answ += SteamwareStrings.addLine(string.Format(" Multicast ............................... : {0}", adapter.SupportsMulticast));
answ += SteamwareStrings.addLine("");
}
}
return answ;
}
}
/// <summary>
/// fornisce elenco dispositivi USB collegati
/// </summary>
/// <returns></returns>
public static string usbDevices
{
get
{
string answ = "";
var usbDevices = GetUSBDevices();
foreach (var usbDevice in usbDevices)
{
answ += SteamwareStrings.addLine(string.Format("Device ID: {0}, PNP Device ID: {1}, Description: {2}", usbDevice.DeviceID, usbDevice.PnpDeviceID, usbDevice.Description));
}
return answ;
}
}
/// <summary>
/// elenco devices USB
/// </summary>
/// <returns></returns>
public static List<USBDeviceInfo> GetUSBDevices()
{
List<USBDeviceInfo> devices = new List<USBDeviceInfo>();
ManagementObjectCollection collection;
using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub")) collection = searcher.Get();
foreach (var device in collection)
{
devices.Add(new USBDeviceInfo(
(string)device.GetPropertyValue("DeviceID"),
(string)device.GetPropertyValue("PNPDeviceID"),
(string)device.GetPropertyValue("Description")
));
}
collection.Dispose();
return devices;
}
/// <summary>
/// elenco porte seriali
/// </summary>
public static string serialPorts
{
get
{
string answ = "";
// Get a list of serial port names.
string[] ports = SerialPort.GetPortNames();
// Display each port name to the console.
foreach (string port in ports)
{
answ += SteamwareStrings.addLine(string.Format("Serial port: {0}", port));
}
return answ;
}
}
/// <summary>
/// registra esito ping ad un dato IP/hostname
/// </summary>
/// <param name="target">IPaddress / host name.</param>
public static string pingIp(string target)
{
string answ = "";
answ += SteamwareStrings.addLine(string.Format("ping to target: {0}", target));
Ping pingSender = new Ping();
PingOptions options = new PingOptions();
// Use the default Ttl value which is 128,
// but change the fragmentation behavior.
options.DontFragment = true;
// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 120;
PingReply reply = pingSender.Send(target, timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
answ += SteamwareStrings.addLine("Success!");
answ += SteamwareStrings.addLine(string.Format(" Address: {0}", reply.Address.ToString()));
answ += SteamwareStrings.addLine(string.Format(" RoundTrip time: {0} ms", reply.RoundtripTime));
answ += SteamwareStrings.addLine(string.Format(" Time to live: {0}", reply.Options.Ttl));
answ += SteamwareStrings.addLine(string.Format(" Don't fragment: {0}", reply.Options.DontFragment));
answ += SteamwareStrings.addLine(string.Format(" Buffer size: {0}", reply.Buffer.Length));
}
return answ;
}
/// <summary>
/// restituisce contenuto di una pagina web (per testing)
/// </summary>
/// <param name="targetUrl"></param>
/// <returns></returns>
public static string getPageContent(string targetUrl)
{
string answ = "";
try
{
//WebClient client = new WebClient();
//answ = client.DownloadString(targetUrl);
WebRequest request = WebRequest.Create(targetUrl);
WebResponse response = request.GetResponse();
Stream data = response.GetResponseStream();
using (StreamReader sr = new StreamReader(data))
{
answ += sr.ReadToEnd();
}
}
catch(Exception exc)
{
answ = string.Format("Exception in recupero pagina: {0}{1}", Environment.NewLine, exc);
}
return answ;
}
}
/// <summary>
/// classe definizione info oggetti USB
/// </summary>
public class USBDeviceInfo
{
/// <summary>
/// Initializes a new instance of the <see cref="USBDeviceInfo"/> class.
/// </summary>
/// <param name="deviceID">The device identifier.</param>
/// <param name="pnpDeviceID">The PNP device identifier.</param>
/// <param name="description">The description.</param>
public USBDeviceInfo(string deviceID, string pnpDeviceID, string description)
{
this.DeviceID = deviceID;
this.PnpDeviceID = pnpDeviceID;
this.Description = description;
}
/// <summary>
/// Gets the device identifier.
/// </summary>
/// <value>
/// The device identifier.
/// </value>
public string DeviceID { get; private set; }
/// <summary>
/// Gets the PNP device identifier.
/// </summary>
/// <value>
/// The PNP device identifier.
/// </value>
public string PnpDeviceID { get; private set; }
/// <summary>
/// Gets the description.
/// </summary>
/// <value>
/// The description.
/// </value>
public string Description { get; private set; }
}
}