Ejemplo n.º 1
0
        private static List<string> BuildCommand(NetworkConfigModel model)
        {
            List<string> commands = new List<string>();
            if (model.UseDHCP)
            {
                commands.Add(string.Format(NETSH_IP_DHCP, model.NetworkName));
            }
            else
            {
                commands.Add(string.Format(NETSH_IP, model.NetworkName, model.IpAddress, model.Subnet, model.Gateway));
            }

            string[] dns = model.DNSList.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            if (dns.Length > 0)
            {
                for (int i = 0; i < dns.Length; i++)
                {
                    if (i == 0)
                    {
                        commands.Add(string.Format(NETSH_DNS, model.NetworkName, dns[i]));
                    }
                    else
                    {
                        commands.Add(string.Format(NETSH_DNS_ADD, model.NetworkName, dns[i], i));
                    }
                }
            }
            else
            {
                commands.Add(string.Format(NETSH_DNS_DHCP, model.NetworkName));
            }

            return commands;
        }
Ejemplo n.º 2
0
        public static bool SaveNICSettings(NetworkConfigModel model)
        {
            try
            {
                List<string> commands = BuildCommand(model);

                foreach (var cmd in commands)
                {
                    Process p = new Process();
                    ProcessStartInfo startInfo = new ProcessStartInfo(NETSH, cmd);
                    startInfo.Verb = "runas";
                    startInfo.CreateNoWindow = true;
                    p.StartInfo = startInfo;
                    p.Start();
                }
                return true;
            }
            catch
            {
                throw;
            }
        }