Ejemplo n.º 1
0
        public static IpEntity CreateDefault(string networkId)
        {
            var model = new IpEntity
            {
                DNS       = "114.114.114.114",
                SpareDNS  = "8.8.8.8",
                NetworkID = networkId
            };
            // ManagementClass mc =new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = new ManagementObjectSearcher("select * from  Win32_NetworkAdapterConfiguration where MACAddress is not null").Get();

            foreach (ManagementObject mo in moc)
            {
                if (!(string.IsNullOrEmpty(model.NetworkID)) && (string)mo["SettingID"] != model.NetworkID)
                {
                    continue;
                }
                if (string.IsNullOrEmpty(model.NetworkID) && (!(bool)mo["IPEnabled"]))
                {
                    continue;
                }
                string[] ips = (string[])mo["DefaultIPGateway"];
                model.Gateway    = ips.Length > 0 ? ips[0] : "";
                ips              = (string[])mo["DNSServerSearchOrder"];
                model.DNS        = ips.Length > 0 ? ips[0] : model.DNS;
                ips              = (string[])mo["IPAddress"];
                model.IpAddress  = ips.Length > 0 ? ips[0] :"";
                ips              = (string[])mo["IPSubnet"];
                model.SubnetMask = ips.Length > 0 ? ips[0] : "";
                break;
            }
            return(model);
        }
Ejemplo n.º 2
0
        public static string DeleteConfig(IpEntity model)
        {
            XmlConfig xmlConfig = LoadItems();

            xmlConfig.Items.RemoveAll(a => a.Id.Equals(model.Id));
            return(SaveConfig(xmlConfig));
        }
Ejemplo n.º 3
0
 public static string UpdateConfig(IpEntity model)
 {
     bool isPass = true;
     var msg = CheckIpEntity(model, ref isPass);
     if (!isPass) { return msg; }
     XmlConfig xmlConfig = LoadItems();
     var index = xmlConfig.Items.FindIndex(a => a.Id.Equals(model.Id));
     if (index < 0 || index > xmlConfig.Items.Count)
     {
         return "更新失败";
     }
     xmlConfig.Items[index] = model;
     return SaveConfig(xmlConfig);
 }
Ejemplo n.º 4
0
        /// <summary>
        /// 添加一个实体
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static string AddConfig(IpEntity model)
        {
            bool isPass = true;
            var  msg    = CheckIpEntity(model, ref isPass);

            if (!isPass)
            {
                return(msg);
            }
            XmlConfig xmlConfig = LoadItems();

            xmlConfig.Items.Add(model);
            return(SaveConfig(xmlConfig));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 修改配置
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static string UpdateConfig(IpEntity model)
        {
            bool isPass = true;
            var  msg    = CheckIpEntity(model, ref isPass);

            if (!isPass)
            {
                return(msg);
            }
            XmlConfig xmlConfig = LoadItems();
            var       index     = xmlConfig.Items.FindIndex(a => a.Id.Equals(model.Id));

            if (index < 0 || index > xmlConfig.Items.Count)
            {
                return("更新失败");
            }
            xmlConfig.Items[index] = model;
            return(SaveConfig(xmlConfig));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 检查实体格式对不对
        /// </summary>
        /// <param name="model"></param>
        /// <param name="isPass"></param>
        /// <returns></returns>
        public static string CheckIpEntity(IpEntity model, ref bool isPass)
        {
            isPass = true;
            var res = string.Empty;

            if (string.IsNullOrEmpty(model.Name))
            {
                isPass = false;
                return("方案名称不能为空!");
            }
            var msg = "IP地址";

            if ((!string.IsNullOrEmpty(model.IpAddress)) && !CheckIps(model.IpAddress, model.SpareIpAddress, ref msg))
            {
                isPass = false;
                return(msg);
            }
            msg = "子网掩码";
            if ((!string.IsNullOrEmpty(model.SubnetMask)) && !CheckIps(model.SubnetMask, model.SpareSubnetMask, ref msg))
            {
                isPass = false;
                return(msg);
            }
            msg = "网关";
            if ((!string.IsNullOrEmpty(model.Gateway)) && !CheckIps(model.Gateway, model.SpareGateway, ref msg))
            {
                isPass = false;
                return(msg);
            }
            msg = "DNS";
            if ((!string.IsNullOrEmpty(model.DNS)) && !CheckIps(model.DNS, model.SpareDNS, ref msg))
            {
                isPass = false;
                return(msg);
            }
            return(string.Empty);
        }
Ejemplo n.º 7
0
 public static string DeleteConfig(IpEntity model)
 {
     XmlConfig xmlConfig = LoadItems();
     xmlConfig.Items.RemoveAll(a=>a.Id.Equals(model.Id));
     return SaveConfig(xmlConfig);
 }
Ejemplo n.º 8
0
        public static string SetNetworkAdapter(IpEntity model,ref bool isOk)
        {
            var res = string.Empty;
            var isPass = true;
            isOk = true;
            res = CheckIpEntity(model, ref isPass);
            if (!isPass)
            {
                isOk = false;
                return res;
            }
            ManagementBaseObject inPar = null;
            ManagementBaseObject outPar = null;
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                if (!(bool)mo["IPEnabled"]) { continue; }
                try
                {
                    string[] ips=null;
                    //设置ip地址和子网掩码 
                    ips = GetIps(model.IpAddress, model.SpareIpAddress);
                    if (ips != null)
                    {
                        inPar = mo.GetMethodParameters("EnableStatic");
                        inPar.SetPropertyValue("IPAddress", ips);
                        var subnet = GetIps(model.SubnetMask, model.SpareSubnetMask);
                        inPar.SetPropertyValue("SubnetMask", subnet); // 1.备用 2.IP
                        outPar = mo.InvokeMethod("EnableStatic", inPar, null);
                        res = GetInvokeMethodMsg(outPar.GetPropertyValue("ReturnValue"),"IP或子网掩码",ref isOk);
                    }

                    //设置网关地址 
                    ips = GetIps(model.Gateway, model.SpareGateway);
                    if (ips != null)
                    {
                        inPar = mo.GetMethodParameters("SetGateways");
                        inPar["DefaultIPGateway"] = ips;// 1.网关;2.备用网关 
                        outPar = mo.InvokeMethod("SetGateways", inPar, null);
                        res += GetInvokeMethodMsg(outPar.GetPropertyValue("ReturnValue"), "网关", ref isOk);
                    }

                    //设置DNS 
                    ips = GetIps(model.DNS, model.SpareDNS);
                    if (ips != null)
                    {
                        inPar = mo.GetMethodParameters("SetDNSServerSearchOrder");
                        inPar["DNSServerSearchOrder"] = ips; // 1.DNS 2.备用DNS 
                        outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null);
                        res += GetInvokeMethodMsg(outPar.GetPropertyValue("ReturnValue"), "DNS", ref isOk);
                    }
                    res=string.IsNullOrEmpty(res)?"OK!":res+ (Environment.NewLine + "Ok!");
                }
                catch (Exception e)
                {
                    res += e.Message;
                    isOk = false;
                    break;
                }
                break;
            }
            return res;
        }
Ejemplo n.º 9
0
 /// <summary>
 /// 检查实体格式对不对
 /// </summary>
 /// <param name="model"></param>
 /// <param name="isPass"></param>
 /// <returns></returns>
 public static string CheckIpEntity(IpEntity model, ref bool isPass)
 {
     isPass = true;
     var res = string.Empty;
     if(string.IsNullOrEmpty(model.Name))
     {
         isPass = false;
         return "方案名称不能为空!";
     }
     var msg = "IP地址";
     if ((!string.IsNullOrEmpty( model.IpAddress))&&!CheckIps(model.IpAddress, model.SpareIpAddress, ref msg))
     {
         isPass = false;
         return msg;
     }
     msg = "子网掩码";
     if ((!string.IsNullOrEmpty(model.SubnetMask)) && !CheckIps(model.SubnetMask, model.SpareSubnetMask, ref msg))
     {
         isPass = false;
         return msg;
     }
     msg = "网关";
     if ((!string.IsNullOrEmpty(model.Gateway)) && !CheckIps(model.Gateway, model.SpareGateway, ref msg))
     {
         isPass = false;
         return msg;
     }
     msg = "DNS";
     if ((!string.IsNullOrEmpty(model.DNS)) && !CheckIps(model.DNS, model.SpareDNS, ref msg))
     {
         isPass = false;
         return msg;
     }
     return string.Empty;
 }
Ejemplo n.º 10
0
        public static IpEntity CreateDefault()
        {
            var model= new IpEntity
            {   
                DNS="114.114.114.114",
                SpareDNS="8.8.8.8",
            };
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                if (!(bool)mo["IPEnabled"]) { continue; }
                string[] ips =(string[]) mo["DefaultIPGateway"];
                model.Gateway = ips.Length > 0 ? ips[0] : "";
                ips = (string[])mo["DNSServerSearchOrder"];
                model.DNS = ips.Length > 0 ? ips[0] : model.DNS;
                ips = (string[])mo["IPAddress"];
                model.IpAddress= ips.Length > 0 ? ips[0] :"";
                ips = (string[])mo["IPSubnet"];
                model.SubnetMask = ips.Length > 0 ? ips[0] : "";
                break;
            }
            return model;

        }
Ejemplo n.º 11
0
 /// <summary>
 /// 添加一个实体
 /// </summary>
 /// <param name="model"></param>
 /// <returns></returns>
 public static string AddConfig(IpEntity model)
 {
     bool isPass = true;
     var msg= CheckIpEntity(model, ref isPass);
     if (!isPass) { return msg; }
     XmlConfig xmlConfig = LoadItems();
     xmlConfig.Items.Add(model);
     return SaveConfig(xmlConfig);
 }
Ejemplo n.º 12
0
 private void saveButton_Click(object sender, EventArgs e)
 {
     var se = itemListBox.SelectedIndex;
     var model = new IpEntity
     {
         Name = nameMaskedTextBox.Text.Trim(),
         IpAddress = ipMaskedTextBox.Text.Trim(),
         Id = idMaskedTextBox.Text.Trim(),
         Gateway = gateMaskedTextBox.Text.Trim(),
         SubnetMask = subnetMaskedTextBox.Text.Trim(),
         DNS = dnsMaskedTextBox.Text.Trim()
     };
     var isPass = true;
     var res = string.Empty;
     res= IpSwitchHelper.CheckIpEntity(model, ref isPass);
     if (!isPass)
     {
         MessageBox.Show(res, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
         return;
     }
     if (model.Id.Equals(tempId)||string.IsNullOrEmpty(model.Id))
     {
         model.Id = Guid.NewGuid().ToString("N");
         res = IpSwitchHelper.AddConfig(model);
     }
     else
     {
         res = IpSwitchHelper.UpdateConfig(model);
     }
     LoadConfig();
     itemListBox.SelectedIndex = se >= 0 ? se : 0;
     MessageBox.Show(res, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
 }
Ejemplo n.º 13
0
        public static string SetNetworkAdapter(IpEntity model, ref bool isOk)
        {
            var res    = string.Empty;
            var isPass = true;

            isOk = true;
            res  = CheckIpEntity(model, ref isPass);
            if (!isPass)
            {
                isOk = false;
                return(res);
            }

            ManagementBaseObject       inPar  = null;
            ManagementBaseObject       outPar = null;
            ManagementObjectCollection moc    = new ManagementObjectSearcher("Select  * from Win32_NetworkAdapterConfiguration where MACAddress is not null and SettingID='" + model.NetworkID + "'").Get();

            if (string.IsNullOrEmpty(model.NetworkID) && moc.Count <= 0)
            {
                isOk = false;
                return("找不到指定网卡");
            }
            foreach (ManagementObject mo in moc)
            {
                if ((string)mo["SettingID"] != model.NetworkID)
                {
                    isOk = false;
                    res += "找不到指定网卡";
                    continue;
                }
                //启用网卡
                if ((!(bool)mo["IPEnabled"]) && (!IpSwitch.Helper.NetworkHelper.EnableNetWork(mo)))
                {
                    isOk = false;
                    return("网卡启用失败");
                }
                try
                {
                    string[] ips = null;
                    //设置ip地址和子网掩码
                    ips = GetIps(model.IpAddress, model.SpareIpAddress);
                    if (ips != null)
                    {
                        inPar = mo.GetMethodParameters("EnableStatic");
                        inPar.SetPropertyValue("IPAddress", ips);
                        var subnet = GetIps(model.SubnetMask, model.SpareSubnetMask);
                        inPar.SetPropertyValue("SubnetMask", subnet); // 1.备用 2.IP
                        outPar = mo.InvokeMethod("EnableStatic", inPar, null);
                        res    = GetInvokeMethodMsg(outPar.GetPropertyValue("ReturnValue"), "IP或子网掩码", ref isOk);
                    }

                    //设置网关地址
                    ips = GetIps(model.Gateway, model.SpareGateway);
                    if (ips != null)
                    {
                        inPar = mo.GetMethodParameters("SetGateways");
                        inPar["DefaultIPGateway"] = ips;// 1.网关;2.备用网关
                        outPar = mo.InvokeMethod("SetGateways", inPar, null);
                        res   += GetInvokeMethodMsg(outPar.GetPropertyValue("ReturnValue"), "网关", ref isOk);
                    }

                    //设置DNS
                    ips = GetIps(model.DNS, model.SpareDNS);
                    if (ips != null)
                    {
                        inPar = mo.GetMethodParameters("SetDNSServerSearchOrder");
                        inPar["DNSServerSearchOrder"] = ips; // 1.DNS 2.备用DNS
                        outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null);
                        res   += GetInvokeMethodMsg(outPar.GetPropertyValue("ReturnValue"), "DNS", ref isOk);
                    }
                    res = string.IsNullOrEmpty(res)?"OK!":res + (Environment.NewLine + "Ok!");
                }
                catch (Exception e)
                {
                    res += e.Message;
                    isOk = false;
                }
                break;
            }
            return(res);
        }
Ejemplo n.º 14
0
 /// <summary>
 /// 获取当前配置项
 /// </summary>
 /// <param name="ls"></param>
 /// <param name="addModel"></param>
 private void GetNowStatus(XmlConfig ls,IpEntity addModel=null)
 {
     var status = string.Empty;
     var defaultModel = IpSwitchHelper.CreateDefault();
     var nowModel = addModel!=null&&addModel!=default(IpEntity)? addModel: ls.Items.Where(a => a.IpAddress.Equals(defaultModel.IpAddress) && a.DNS.Equals(defaultModel.DNS) && a.SubnetMask.Equals(defaultModel.SubnetMask) && a.Gateway.Equals(defaultModel.Gateway)).FirstOrDefault();
     if (nowModel!=default(IpEntity))
     {
         status = nowModel.Name;
         for (int i = 0; i < itemsComboBox.Items.Count; i++)
         {
             var ipEntity = (IpEntity)itemsComboBox.Items[i];
             if (ipEntity.Id.Equals(nowModel.Id))
             {
                 itemsComboBox.SelectedIndex = i;
             }
         }
         formNotifyIcon.ContextMenuStrip.Items.RemoveAt(0);
         formNotifyIcon.ContextMenuStrip.Items.Insert(0,new ToolStripMenuItem("当前:"+status));
     }
     statusLabel.Text = status;
 }