private void ScanDev_DoWork(object sender, DoWorkEventArgs e) { object[] args = (object[])e.Argument; int begin = Convert.ToInt32(args[1]); int end = Convert.ToInt32(args[2]); List <string> ips = new List <string>(); string ipHeader = args[0].ToString(); ScanDeviceManager sdm = new ScanDeviceManager(); sdm.Run(ipHeader, begin, end); while (true) { if (sdm.Finish) { string[] res = sdm.IPS.Split(';'); for (int i = 0; i < res.Length; i++) { if (!string.IsNullOrEmpty(res[i])) { if (GeneralMethod.ConnectIPC(res[i])) { ips.Add(res[i]); } } } break; } Thread.Sleep(500); } e.Result = ips; }
private void ConnectAP_Completed(object sender, RunWorkerCompletedEventArgs e) { try { object[] res = (object[])e.Result; if ((bool)res[0])//连接成功 { List <string> ips = GeneralMethod.GetLocalIPS(); localIP = ips[0]; ip_begin_h.Text = ips[0].Substring(0, ips[0].LastIndexOf('.') + 1); ip_end_h.Text = ips[0].Substring(0, ips[0].LastIndexOf('.') + 1); btn_scan.IsEnabled = true; btn_connect.Content = "网络已连接"; btn_connect.IsEnabled = false; btn_ok.IsEnabled = true; btn_connect.Foreground = new SolidColorBrush(Colors.Green); } else//连接失败 { LogManager.Insert(this, "ConnectAP_Completed", res[1].ToString()); MsgBox.ShowV2(res[1].ToString(), MsgBoxType.Error, this); btn_scan.IsEnabled = false; btn_ok.IsEnabled = false; btn_connect.IsEnabled = true; } } catch (Exception ex) { string errMsg = ""; if (ex.InnerException != null) { errMsg = ex.Message + "(" + ex.InnerException.Message + ")"; } else { errMsg = ex.Message; } LogManager.Insert(this, "ConnectAP_Completed", errMsg); MsgBox.ShowV2(errMsg, MsgBoxType.Error, this); } finally { if (sender != null)//捕获异常后手动关闭线程 { BackgroundWorker worker = sender as BackgroundWorker; if (worker.WorkerSupportsCancellation && !worker.CancellationPending) { worker.CancelAsync(); } worker.Dispose(); } if (waitBox != null) { waitBox.Close(); } } }
private void Worker() { for (int i = ipBegin; i < ipEnd; i++) { string ip = ipHeader + i; if (GeneralMethod.Ping(ip)) { ips += ip + ";"; } } finish = true; }
private void MainWindow_Loaded(object sender, RoutedEventArgs e) { try { LocalParams lps = GeneralMethod.GetLocalParams(); string[] ipss = new string[] { lps.IP }; string[] marks = new string[] { lps.Mark }; string[] gateWays = new string[] { "192.168.0.1" }; APManager.SetIPAddress(ipss, marks, gateWays, null); btn_ok.IsEnabled = false; if (!File.Exists(SystemSettings.ConfigPath))//未初始化 { btn_scan.IsEnabled = false; } else { SystemSettings config = SystemSettings.Read(); if (config != null) { ShowConfig(config); } //如果网络已经连接 string errMsg = ""; if (APManager.ConnectToSSID(config.APUser, config.APPass, out errMsg)) { List <string> ips = GeneralMethod.GetLocalIPS(); localIP = ips[0]; ip_begin_h.Text = ips[0].Substring(0, ips[0].LastIndexOf('.') + 1); ip_end_h.Text = ips[0].Substring(0, ips[0].LastIndexOf('.') + 1); btn_scan.IsEnabled = true; btn_connect.Content = "网络已连接"; btn_connect.Foreground = new SolidColorBrush(Colors.Green); btn_connect.IsEnabled = false; } else { btn_scan.IsEnabled = false; btn_ok.IsEnabled = false; LogManager.Insert(this, "MainWindow_Loaded", "网络连接异常,请重新连接!"); MsgBox.ShowV2("网络连接异常,请重新连接!", MsgBoxType.Error, this); } } } catch (Exception ex) { LogManager.Insert(this, "MainWindow_Loaded", ex.Message); MsgBox.ShowV2(ex.Message, MsgBoxType.Error, this); } }
public static string GetGatewayIP() { string strGateway = ""; //获取所有网卡 NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); //遍历数组 foreach (var netWork in nics) { //单个网卡的IP对象 IPInterfaceProperties ip = netWork.GetIPProperties(); //获取该IP对象的网关 GatewayIPAddressInformationCollection gateways = ip.GatewayAddresses; foreach (var gateWay in gateways) { //如果能够Ping通网关 if (GeneralMethod.Ping(gateWay.Address.ToString())) { //得到网关地址 strGateway = gateWay.Address.ToString(); //跳出循环 break; } } //如果已经得到网关地址 if (strGateway.Length > 0) { //跳出循环 break; } } return(strGateway); }