Exemplo n.º 1
0
        /// <summary>
        /// Returns the network device configuration of the specified NIC
        /// </summary>
        /// <param name="deviceName">Name of the Network device</param>
        public static async Task <WMIAdapter> GetIPAsync(string deviceName)
        {
            return(await Task.Run(() =>
            {
                WMIAdapter wmiAdapter = new WMIAdapter()
                {
                    deviceName = deviceName
                };

                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();

                foreach (ManagementObject mo in moc)
                {
                    // Make sure this is a IP enabled device. Not something like memory card or VM Ware
                    if ((bool)mo["ipEnabled"])
                    {
                        if (mo["Caption"].Equals(deviceName))
                        {
                            wmiAdapter.ipAdresses = (string[])mo["IPAddress"];
                            wmiAdapter.subnets = (string[])mo["IPSubnet"];
                            wmiAdapter.gateways = (string[])mo["DefaultIPGateway"];
                            wmiAdapter.dnses = (string[])mo["DNSServerSearchOrder"];

                            break;
                        }
                    }
                }
                return wmiAdapter;
            }));
        }
Exemplo n.º 2
0
        public static async Task LogNetworkEventAsync(NetworkManager.Core.Services.WMIAdapter wmiAdapter)
        {
            await Task.Run(() =>
            {
                string ips      = NetworkConfigValidator.ConvertArrayOfIpsToString(wmiAdapter.ipAdresses);
                string subnets  = NetworkConfigValidator.ConvertArrayOfIpsToString(wmiAdapter.subnets);
                string gateways = NetworkConfigValidator.ConvertArrayOfIpsToString(wmiAdapter.gateways);
                string dnss     = NetworkConfigValidator.ConvertArrayOfIpsToString(wmiAdapter.dnses);

                using (StreamWriter w = File.AppendText(_filePath + _fileName))
                {
                    w.WriteLine("  Device:{0} ip:{1} subnet:{2}, gateway:{3} dns:{4} ", wmiAdapter.deviceName, ips, subnets, gateways, dnss);
                    w.WriteLine("Config changed: {0} {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString());
                    w.Close();
                }
            });
        }