Files
spark/src/DataStreamSuite/PubSub/Misc/SysInfoManager.cs
T

87 lines
2.5 KiB
C#

using bts;
using System;
//using System.Windows.Forms;
using System.Collections;
//using System.Management;
using System.Net;
namespace BCG.Utilities.General
{
/// <summary>
/// Contains utlitiy functions for getting System info
/// </summary>
public class SysInfoManager
{
public SysInfoManager()
{
}
// public static string getIPWMI()
// {
// string[] adds = getIPSWMI();
// if (adds.Length > 1)
// {
// throw new Exception("Mulitple network cards");
// }
// else return (string)adds[0];
// }
// public static string[] getIPSWMI()
// {
// ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration");
// ManagementObjectCollection queryCollection = query.Get ();
// ArrayList adds = new ArrayList();
// foreach( ManagementObject mo in queryCollection )
// {
// string[] addresses = (string[])mo["IPAddress"];
// if((bool)mo["IpEnabled"])
// foreach(string s in addresses)
// adds.Add (s);
// }
// return (string[])adds.ToArray(typeof(string));
// }
public static string getIP()
{
// TODOds
try {
string hostName = Dns.GetHostName();
// Get the IP addresses associated with the host name
IPAddress[] ipAddresses = Dns.GetHostAddresses(hostName);
return ipAddresses[0].ToString();
} catch (Exception ex) {
Log.assert(false, $"Dns.GetHostName threw: {ex}");
return string.Empty;
}
// string cName = SystemInformation.ComputerName;
// IPAddress[] ipas;
// try
// {
// ipas = Dns.GetHostEntry(cName).AddressList;
// }
// catch(ArgumentNullException ane)
// {
// throw new Exception(ane.Message);
// }
// catch(System.Net.Sockets.SocketException se)
// {
// throw new Exception(se.Message);
// }
// if(ipas == null || ipas.Length == 0)
// {
// string msg = "Could not retreive the IP address of this machine, please contact IT";
// throw new Exception(msg);
// }
// return ipas[0].ToString();
}
public static string getHostNameFromIP (string ip)
{
return Dns.GetHostEntry (ip).HostName.Split('.')[0];
}
}
}