Exemplo n.º 1
0
        private void TsEditLis_Click(object sender, EventArgs e)
        {
            if (gvLis.SelectedRows.Count > 0)
            {
                DataGridViewRow row     = gvLis.SelectedRows[0];
                LanMachine      machine = row.Tag as LanMachine;
                if (machine == null)
                {
                    return;
                }

                string nickName = FrmInputBox.ShowInputBox("请输入机器昵称");
                if (string.IsNullOrWhiteSpace(nickName))
                {
                    return;
                }
                machine.NickName = nickName.Trim();
                SetCellValue("ColLisName", machine.NickName, row);
                RefreashWeb();
                try
                {
                    SaveLisTo(LastListenFile);
                }
                catch { }
            }
        }
Exemplo n.º 2
0
 private void DoPing()
 {
     while (!_isStop)
     {
         PingReply rep = LanMachine.Ping(_ip, 1000);
         string    msg = GetMessage(rep);
         try
         {
             if (!this.IsDisposed && !_isStop)
             {
                 this.Invoke(new MethodInvoker(delegate
                 {
                     if (rtbInfo.Text.Length > 8000)
                     {
                         rtbInfo.Text = "";
                     }
                     rtbInfo.Text          += msg + "\n\n";
                     rtbInfo.SelectionStart = rtbInfo.Text.Length;
                 }));
             }
         }
         catch { }
         Thread.Sleep(1000);
     }
 }
Exemplo n.º 3
0
 private void AddToGrid(LanMachine machine, DataGridView dgv)
 {
     this.Invoke(new MethodInvoker(delegate
     {
         DataGridViewRow row = GetRow(machine, dgv);
     }));
 }
Exemplo n.º 4
0
        private void TsAddToLis_Click(object sender, EventArgs e)
        {
            if (dgMembers.SelectedRows.Count > 0)
            {
                DataGridViewRow row     = dgMembers.SelectedRows[0];
                LanMachine      machine = row.Tag as LanMachine;
                if (machine == null)
                {
                    return;
                }

                string nickName = FrmInputBox.ShowInputBox("请输入机器昵称");
                if (string.IsNullOrWhiteSpace(nickName))
                {
                    return;
                }
                machine.NickName = nickName.Trim();
                DataGridViewRow newrow = GetRow(machine, gvLis);
                RefreashWeb();
                try
                {
                    SaveLisTo(LastListenFile);
                }
                catch { }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// 获取y已存在的机器行
        /// </summary>
        /// <param name="machine"></param>
        /// <returns></returns>
        private DataGridViewRow GetRow(LanMachine machine, DataGridView dgv)
        {
            foreach (DataGridViewRow currow in dgv.Rows)
            {
                LanMachine curmachine = currow.Tag as LanMachine;
                if (curmachine == null)
                {
                    continue;
                }
                if (curmachine.Mac.Mac == machine.Mac.Mac)
                {
                    if (curmachine.IP != machine.IP)
                    {
                        curmachine.IP       = machine.IP;
                        curmachine.HostName = machine.HostName;

                        SetCellValue("ColName", machine.HostName, currow);
                        SetCellValue("ColIP", machine.IP.ToString(), currow);
                        SetCellValue("ColLisName", machine.NickName, currow);
                    }
                    return(currow);
                }
            }
            int             i      = dgv.Rows.Add();
            DataGridViewRow newrow = dgv.Rows[i];

            SetCellValue("ColIP", machine.IP, newrow);
            SetCellValue("ColMAC", machine.Mac.ToString(), newrow);
            SetCellValue("ColLisMAC", machine.Mac.ToString(), newrow);
            SetCellValue("ColName", machine.HostName, newrow);
            SetCellValue("ColLisName", machine.NickName, newrow);

            newrow.Tag = machine;
            return(newrow);
        }
Exemplo n.º 6
0
        /// <summary>
        /// 加载文件
        /// </summary>
        /// <returns></returns>
        private void LoadListenInfo(string path)
        {
            List <LanMachine> lstMachine = LanMachine.LoadList(path);

            gvLis.Rows.Clear();
            foreach (LanMachine machine in lstMachine)
            {
                AddToGrid(machine, gvLis);
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// 保存监听信息到文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        private bool SaveLisTo(string path)
        {
            List <LanMachine> lstMachine = GetAllMachine(gvLis);

            if (lstMachine.Count == 0)
            {
                return(false);
            }
            LanMachine.SaveTo(path, lstMachine);
            return(true);
        }
Exemplo n.º 8
0
 private void 唤醒ToolStripMenuItem_Click(object sender, EventArgs e)
 {
     if (dgMembers.SelectedRows.Count > 0)
     {
         DataGridViewRow row     = dgMembers.SelectedRows[0];
         LanMachine      machine = row.Tag as LanMachine;
         if (machine == null)
         {
             return;
         }
         machine.WakeOnLan();
     }
 }
Exemplo n.º 9
0
 private void TsCreateSnapPing_Click(object sender, EventArgs e)
 {
     if (dgMembers.SelectedRows.Count > 0)
     {
         DataGridViewRow row     = dgMembers.SelectedRows[0];
         LanMachine      machine = row.Tag as LanMachine;
         if (machine == null)
         {
             return;
         }
         machine.CreateWakeOnSnapshot(true);
     }
 }
Exemplo n.º 10
0
        /// <summary>
        /// 唤醒
        /// </summary>
        /// <param name="mac"></param>
        private void WakeOn(string strmac)
        {
            long mac = 0;

            if (!long.TryParse(strmac, out mac))
            {
                return;
            }
            LanMachine machine = new LanMachine();

            machine.Mac = new MacInfo(mac);
            machine.WakeOnLan();
        }
Exemplo n.º 11
0
 private void  制ToolStripMenuItem_Click(object sender, EventArgs e)
 {
     if (dgMembers.SelectedRows.Count > 0)
     {
         DataGridViewRow row     = dgMembers.SelectedRows[0];
         LanMachine      machine = row.Tag as LanMachine;
         if (machine == null)
         {
             return;
         }
         string str = machine.IP.ToString() + "   " + machine.HostName + "   " + machine.Mac;
         Clipboard.SetDataObject(str);
     }
 }
Exemplo n.º 12
0
        /// <summary>
        /// 获取所有机器信息
        /// </summary>
        /// <returns></returns>
        private List <LanMachine> GetAllMachine(DataGridView dgv)
        {
            List <LanMachine> lstMachine = new List <LanMachine>();

            foreach (DataGridViewRow row in dgv.Rows)
            {
                LanMachine machine = row.Tag as LanMachine;
                if (machine == null)
                {
                    continue;
                }
                lstMachine.Add(machine);
            }
            return(lstMachine);
        }
Exemplo n.º 13
0
        /// <summary>
        /// 保存到文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="lst"></param>
        public static List <LanMachine> LoadList(string filePath)
        {
            XmlDocument xml = new XmlDocument();

            xml.Load(filePath);
            List <LanMachine> lst   = new List <LanMachine>();
            XmlNodeList       nodes = xml.GetElementsByTagName("LanMachine");

            foreach (XmlNode node in nodes)
            {
                LanMachine machine = new LanMachine();
                machine.Load(node);
                lst.Add(machine);
            }
            return(lst);
        }
Exemplo n.º 14
0
        /// <summary>
        /// 绑定IP
        /// </summary>
        private void BindIP()
        {
            //cmbIP.Items.Clear();
            List <LanMachine> lst = LanMachine.GetLocalInfos();

            //foreach (LanMachine machine in lst)
            //{
            //    cmbIP.Items.Add(machine);
            //}
            cmbIP.DisplayMember = "IP";
            cmbIP.ValueMember   = "IP";
            cmbIP.DataSource    = lst;
            if (cmbIP.Items.Count > 0)
            {
                cmbIP.SelectedIndex = 0;
            }
        }
Exemplo n.º 15
0
        /// <summary>
        /// 扫描机器
        /// </summary>
        /// <param name="machine"></param>
        private void ScanMachine(object objMachine)
        {
            LanMachine machine = objMachine as LanMachine;

            try
            {
                if (machine.Ping(_speed.Timeout).Status == IPStatus.Success)
                {
                    try
                    {
                        machine.HostName = LanMachine.GetHostName(machine.IP);
                    }
                    catch
                    {
                        machine.HostName = machine.IP.ToString();
                    }
                    try
                    {
                        machine.Mac = MacInfo.GetRemoteMac(machine.IP.ToString());
                    }
                    catch { }
                    AddToGrid(machine, dgMembers);
                }
            }
            catch (Exception ex) { }
            finally
            {
                try
                {
                    if (!this.IsDisposed && !_isStop)
                    {
                        this.Invoke(new MethodInvoker(delegate
                        {
                            tspbScan.Value++;
                            tsProcess.Text = (tspbScan.Value * 100 / tspbScan.Maximum).ToString() + "%";
                            if (tspbScan.Value >= tspbScan.Maximum)
                            {
                                DoScaning(false);
                            }
                        }));
                    }
                }
                catch { }
            }
        }
Exemplo n.º 16
0
        /// <summary>
        /// 获取ping结果消息
        /// </summary>
        /// <param name="rep"></param>
        /// <returns></returns>
        private string GetMessage(PingReply reply)
        {
            StringBuilder sbInfo = new StringBuilder();

            sbInfo.Append(string.Format("主机: {0}  ", reply.Address));
            if (reply.Status == IPStatus.Success)
            {
                sbInfo.Append(string.Format("响应时间: {0}毫秒  ", reply.RoundtripTime));
                sbInfo.Append(string.Format("TTL: {0}  ", reply.Options.Ttl));
                //sbInfo.Append("Don't fragment: {0}", reply.Options.DontFragment);
                sbInfo.Append(string.Format("发送字节: {0}", reply.Buffer.Length));
            }
            else
            {
                sbInfo.Append(LanMachine.GetPingError(reply.Status));
            }
            return(sbInfo.ToString());
        }
Exemplo n.º 17
0
 private void 查看共享ToolStripMenuItem_Click(object sender, EventArgs e)
 {
     if (dgMembers.SelectedRows.Count > 0)
     {
         DataGridViewRow row     = dgMembers.SelectedRows[0];
         LanMachine      machine = row.Tag as LanMachine;
         if (machine == null)
         {
             return;
         }
         string ip = machine.IP.ToString();
         if (!string.IsNullOrEmpty(ip))
         {
             FrmPing frm = new FrmPing();
             OpenShare(ip);
         }
     }
 }
Exemplo n.º 18
0
        private void cmbIP_SelectedIndexChanged(object sender, EventArgs e)
        {
            LanMachine localMachine = cmbIP.SelectedItem as LanMachine;

            if (localMachine == null)
            {
                return;
            }
            _localMachine = localMachine;
            byte[]        arrIP = _localMachine.IP.GetAddressBytes();
            StringBuilder sbIP  = new StringBuilder();

            for (int i = 0; i < 3; i++)
            {
                sbIP.Append(arrIP[i].ToString());
                sbIP.Append(".");
            }
            txtStar.Value = sbIP.ToString() + "1";
            txtEnd.Value  = sbIP.ToString() + "254";
            gpScan.Text   = "扫描局域网:" + "网络IP:" + _localMachine.IP.ToString();
        }
Exemplo n.º 19
0
        /// <summary>
        /// 开始扫描
        /// </summary>
        private void DoScan()
        {
            int numStar = 0;
            int numEnd  = 0;

            _lstThreads.Clear();
            string head = GetInfo(ref numStar, ref numEnd);

            for (int i = numStar; i <= numEnd; i++)
            {
                if (_isStop)
                {
                    break;
                }
                LanMachine machine = new LanMachine();
                machine.IP = IPAddress.Parse(head + i.ToString());
                Thread thd = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(ScanMachine));
                _lstThreads.Enqueue(thd);
                thd.Start(machine);
                Thread.Sleep(_speed.SleepTime);
            }
        }
Exemplo n.º 20
0
        /// <summary>
        /// 命令行网络唤醒功能
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        private static bool DoWakeOn(string[] args)
        {
            if (args == null || args.Length < 2)
            {
                return(false);
            }
            if (!string.Equals(args[0], "-wakeon", StringComparison.CurrentCultureIgnoreCase))
            {
                return(false);
            }

            long mac = 0;

            if (!long.TryParse(args[1], out mac))
            {
                return(false);
            }
            LanMachine machine = new LanMachine();

            machine.Mac = new MacInfo(mac);
            machine.WakeOnLan();
            return(true);
        }
Exemplo n.º 21
0
        /// <summary>
        /// 获取所有网卡信息
        /// </summary>
        /// <returns></returns>
        public static List <LanMachine> GetLocalInfos()
        {
            List <LanMachine> lstMachine = new List <LanMachine>();
            string            hostName   = Dns.GetHostName();                        //得到本机的主机名

            System.Net.IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(hostName);; //取得本机IP

            foreach (IPAddress ip in ipEntry.AddressList)
            {
                try
                {
                    LanMachine machine = new LanMachine();
                    //获取IP

                    machine.IP       = ip;
                    machine.HostName = hostName;

                    //machine.Mac = GetRemoteMac(machine.IP.ToString(), machine.IP.ToString());
                    lstMachine.Add(machine);
                }
                catch { }
            }
            return(lstMachine);
        }
Exemplo n.º 22
0
        public override bool Equals(object obj)
        {
            LanMachine machine = obj as LanMachine;

            if (machine == null)
            {
                return(false);
            }
            byte[] ip1 = this.IP.GetAddressBytes();
            byte[] ip2 = machine.IP.GetAddressBytes();
            if (ip1.Length != ip2.Length)
            {
                return(false);
            }
            for (int i = 0; i < ip1.Length; i++)
            {
                if (ip1[i] != ip2[i])
                {
                    return(false);
                }
            }

            return(this.Mac.Mac == machine.Mac.Mac);
        }