예제 #1
0
        /// <summary>
        /// Set IP for the specified network card name
        /// </summary>
        /// <param name="deviceName">Caption of the network card</param>
        /// <param name="IpAddresses">Comma delimited string containing one or more IP</param>
        /// <param name="SubnetMask">Subnet mask</param>
        /// <param name="Gateway">Gateway IP</param>
        /// <param name="Dns">Comma delimited DNS IP</param>
        public async Task <WMIAdapter> SetIPAsync(string deviceName, string IpAddresses, string SubnetMask, string Gateway, string Dns)
        {
            return(await Task.Run(async() =>
            {
                using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
                {
                    using (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))
                                {
                                    try
                                    {
                                        //ManagementBaseObject newGate = mo.GetMethodParameters("SetGateways");
                                        //newGate["DefaultIPGateway"] = new string[] { "192.168.0.1" };
                                        //newGate["GatewayCostMetric"] = new int[] { 1 };
                                        //ManagementBaseObject newDNS = mo.GetMethodParameters("SetDNSServerSearchOrder");
                                        //newDNS["DNSServerSearchOrder"] = new string[] { "192.168.0.1" };
                                        //ManagementBaseObject setGateways = mo.InvokeMethod("SetGateways", newGate, null);
                                        //ManagementBaseObject setDNS = mo.InvokeMethod("SetDNSServerSearchOrder", newDNS, null);
                                        ManagementBaseObject newIP = mo.GetMethodParameters("EnableStatic");
                                        if (NetworkConfigValidator.isValidIP(IpAddresses))
                                        {
                                            newIP["IPAddress"] = IpAddresses.Split(',');
                                            var IpSubnetIps = (string[])mo["IPSubnet"];
                                            newIP["SubnetMask"] = IpSubnetIps[0].Split(',');
                                        }

                                        ManagementBaseObject setIP = mo.InvokeMethod("EnableStatic", newIP, null);
                                        Logging.Logger.Instanse.Log("Info", "Setting device configuration: Device: " + deviceName +
                                                                    " Ip: " + NetworkConfigValidator.ConvertArrayOfIpsToString(IpAddresses.Split(',')));
                                    }
                                    catch (Exception ex)
                                    {
                                        Logging.Logger.Instanse.Log("Warn", "Unable to Set IP : ", ex);
                                    }

                                    break;
                                }
                            }
                        }
                    }
                }
                return await GetIPAsync(deviceName);
            }));
        }
예제 #2
0
        /// <summary>
        /// Returns the network device configuration of the specified NIC
        /// </summary>
        /// <param name="deviceName">Name of the Network device</param>
        public async Task <WMIAdapter> GetIPAsync(string deviceName)
        {
            return(await Task.Run(() =>
            {
                using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
                {
                    using (ManagementObjectCollection moc = mc.GetInstances())
                    {
                        WMIAdapter wmiAdapter = new WMIAdapter()
                        {
                            deviceName = deviceName
                        };
                        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;
                                }
                            }
                        }
                        if (wmiAdapter != null)
                        {
                            Logging.Logger.Instanse.Log("Info", "Request for getting IP of " + deviceName +
                                                        " Response: IPAddress:" + NetworkConfigValidator.ConvertArrayOfIpsToString(wmiAdapter.ipAdresses) + " IPSubnet:" + NetworkConfigValidator.ConvertArrayOfIpsToString(wmiAdapter.subnets) +
                                                        " DefaultIPGateway:" + NetworkConfigValidator.ConvertArrayOfIpsToString(wmiAdapter.gateways) + " Dns: " + NetworkConfigValidator.ConvertArrayOfIpsToString(wmiAdapter.dnses));
                            return wmiAdapter;
                        }
                        else
                        {
                            Logging.Logger.Instanse.Log("Error", "Request for getting IP of " + deviceName + " Response: null");
                            return wmiAdapter;
                        }
                    }
                }
            }));
        }
예제 #3
0
        public WMIAdapter SetIp(string deviceName, string IpAddresses, string SubnetMask, string Gateway, string Dns)
        {
            using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
            {
                using (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))
                            {
                                try
                                {
                                    ManagementBaseObject newIP = mo.GetMethodParameters("EnableStatic");
                                    if (NetworkConfigValidator.isValidIP(IpAddresses))
                                    {
                                        newIP["IPAddress"] = IpAddresses.Split(',');
                                        var IpSubnetIps = (string[])mo["IPSubnet"];
                                        newIP["SubnetMask"] = IpSubnetIps[0].Split(',');
                                    }

                                    ManagementBaseObject setIP = mo.InvokeMethod("EnableStatic", newIP, null);
                                    Logging.Logger.Instanse.Log("Info", "Setting device configuration: Device: " + deviceName +
                                                                " Ip: " + NetworkConfigValidator.ConvertArrayOfIpsToString(IpAddresses.Split(',')));
                                }
                                catch (Exception ex)
                                {
                                    Logging.Logger.Instanse.Log("Warn", "Unable to Set IP : ", ex);
                                }

                                break;
                            }
                        }
                    }
                }
            }
            return(GetIP(deviceName));
        }