private void btnSearchUsers_Click(object sender, EventArgs e)
        {
            bool   result = false;
            string ip     = "192.168.161.10";

            result = MvNetworker.isPingAlive(ip);
            if (result == false)
            {
                Console.WriteLine(string.Format("{0} is not alive", ip));
            }
            else
            {
                Console.WriteLine(string.Format("{0} is alive", ip));
            }


            searchUserByWinPC();
            GetComputerUsers();

            //searchUsersByLDAP(MvAdConnector.ConnectionString_LDAP, MvAdConnector.DomainOffice);
            findAllUsers();
            string test1 = ValidateUser(MvAdConnector.ConnectionString_LDAP, MvAdConnector.DomainOffice);

            searchUserByWinPC();
        }
        private void btnGetIPAliveByIpRange_Click(object sender, EventArgs e)
        {
            DataTable dt = null;

            IPEnumeration IPList = new IPEnumeration("192.168.161.0", "192.168.161.255");

            MvLogger.write("start process");
            MvItToolBo bo = new MvItToolBo();

            dt = bo.scanComputerByIpRange(new string[] { "192.168.161" }, true);
            MvLogger.write("end process");
            Console.WriteLine(dt.Rows.Count);


            richTextBox1.Clear();


            foreach (IPAddress ip in IPList)
            {
                bool   result   = MvNetworker.isPingAlive(ip);
                string hostName = string.Empty;
                string strShow  = string.Empty;

                if (result == false)
                {
                    strShow = string.Format("{0} is not alive{1}", ip.ToString(), Environment.NewLine);
                    Console.WriteLine(strShow);
                    richTextBox1.Text += strShow;
                    richTextBox1.Refresh();

                    continue;
                }

                try
                {
                    // 取host name
                    IPHostEntry host = Dns.GetHostEntry(ip);
                    hostName = host.HostName;
                    strShow  = string.Format("{0} {1} is alive{2}", ip.ToString(), hostName, Environment.NewLine);
                    Console.WriteLine(strShow);
                    richTextBox1.Text += strShow;
                    richTextBox1.Refresh();
                }
                catch (SocketException)
                {
                    strShow = string.Format("{0} host name is not alive{1}", ip.ToString(), Environment.NewLine);
                    Console.WriteLine(strShow);
                    richTextBox1.Text += strShow;
                    richTextBox1.Refresh();
                }
            }
        }
        /// <summary>
        /// 取得Mv網段 alive PC 與DB Table PCList的差異
        /// </summary>
        /// <returns></returns>
        public DataTable compareMvNetworkByPcName(string[] ipRange)
        {
            DataTable resultData = new DataTable("Result");

            // 定義result data的header
            foreach (string header in mvPCListHeader)
            {
                resultData.Columns.Add(header);
            }
            // 忽略大小寫
            Hashtable hashByIp   = new Hashtable(StringComparer.OrdinalIgnoreCase);
            Hashtable hashByName = new Hashtable(StringComparer.OrdinalIgnoreCase);

            // 先用IP掃描所有網段
            foreach (string ipLan in ipRange)
            {
                // 取得IP Lan 範圍
                IPEnumeration IPList = null;
                IPList = getIpRange(ipLan);

                string hostName = string.Empty;
                foreach (IPAddress ip in IPList)
                {
                    bool result = MvNetworker.isPingAlive(ip);
                    // ping 沒回應, 繼續下個IP
                    if (result == false)
                    {
                        continue;
                    }

                    try
                    {
                        // 取host name
                        IPHostEntry host = Dns.GetHostEntry(ip);
                        hostName = host.HostName;

                        hashByIp.Add(ip.ToString(), hostName);
                        // 只有取的到hostname的, 才放入hashByName, 供後續比對使用
                        hashByName.Add(hostName, ip.ToString());
                    }
                    catch (SocketException)
                    {
                        hashByIp.Add(ip.ToString(), hostName);
                    }
                }
            }
            // 用itReportLog內的資料判斷是否相同及存在
            DataTable pcList = MvDbDao.queryData_ItReportLog();
            // 因為IP會一直改變, 但DomainName不會變
            // 此部份的判斷邏輯會以DomainName為主
            string ipInTable     = string.Empty;
            string pcNameInTable = string.Empty;
            string pcNameInTableWithoutDomain = string.Empty;
            string pcNameInHash   = string.Empty;
            string ipInHashByName = string.Empty;

            // 1. 由Table PCList 比對 掃描網段的資料
            foreach (DataRow dr in pcList.Rows)
            {
                ipInTable = dr["NetworkIP"].ToString();
                pcNameInTableWithoutDomain = dr["PCName"].ToString();
                pcNameInTable = string.Format("{0}.{1}.{2}", dr["PCName"].ToString(), MvAdConnector.DomainOffice, MvAdConnector.DomainMv);

                DataRow newDr = resultData.NewRow();
                // 先用DomainName判斷是否存在Hash Table內
                // 不存在, 直接判斷該record資料已失效
                if (hashByName.Contains(pcNameInTable) == false)
                {
                    newDr["NetworkIP"]    = ipInTable;
                    newDr["PCName"]       = pcNameInTable;
                    newDr["IsSamePCName"] = "N";
                    newDr["Action"]       = "Delete";
                    newDr["Remark"]       = string.Empty;
                    resultData.Rows.Add(newDr);
                    continue;
                }
                // 存在, 取出IP做後續判斷
                ipInHashByName = hashByName[pcNameInTable].ToString();
                if (ipInHashByName.Equals(dr["NetworkIP"]) == true)
                {
                    // 判斷IP相同, 回傳不需修改
                    newDr["NetworkIP"]    = ipInTable;
                    newDr["PCName"]       = pcNameInTable;
                    newDr["IsSamePCName"] = "Y";
                    newDr["Action"]       = string.Empty;
                    newDr["Remark"]       = string.Empty;
                    hashByIp.Remove(ipInTable);
                }
                else
                {
                    // 判斷IP不同, 將要變更IP的結果存至Action
                    newDr["NetworkIP"]    = ipInTable;
                    newDr["PCName"]       = pcNameInTable;
                    newDr["IsSamePCName"] = "Y";
                    newDr["Action"]       = "ChangeIp";
                    newDr["Remark"]       = ipInHashByName;
                    // 並將要變更的IP從hash中移除
                    hashByIp.Remove(ipInHashByName);
                }
                resultData.Rows.Add(newDr);
            }
            // 2. 未被移除的資料, 即為新增, 需放入Result data
            foreach (DictionaryEntry de in hashByIp)
            {
                DataRow newDr = resultData.NewRow();
                newDr["NetworkIP"]    = de.Key;
                newDr["PCName"]       = de.Value.ToString().Replace(string.Format(".{0}.{1}", MvAdConnector.DomainOffice, MvAdConnector.DomainMv), "");
                newDr["IsSamePCName"] = "N";
                newDr["Action"]       = "New";
                newDr["Remark"]       = string.Empty;
                resultData.Rows.Add(newDr);
            }
            return(resultData);
        }
        /// <summary>
        /// 掃描網段內所有IP是否有回應
        /// </summary>
        /// <param name="ipRange">IP網段</param>
        /// <returns></returns>
        public DataTable scanComputerByIpRange(string[] ipRange, bool isOnlyReturnSuccess = true)
        {
            DataTable resultData = new DataTable("Result");

            // 定義result data的header
            foreach (string header in mvPCListHeader)
            {
                resultData.Columns.Add(header);
            }

            Hashtable hashByIp = new Hashtable();

            // 先用IP掃描所有網段
            foreach (string ipLan in ipRange)
            {
                // 取得IP Lan 範圍
                IPEnumeration IPList;
                IPList = getIpRange(ipLan);

                // 執行掃描作業
                string hostName = string.Empty;
                foreach (IPAddress ip in IPList)
                {
                    DataRow newDr  = resultData.NewRow();
                    bool    result = MvNetworker.isPingAlive(ip);
                    // ping 沒回應, 繼續下個IP
                    if (result == false)
                    {
                        if (isOnlyReturnSuccess == false)
                        {
                            newDr["NetworkIP"]    = ip.ToString();
                            newDr["PCName"]       = "--";
                            newDr["IsSamePCName"] = "--";
                            newDr["Action"]       = "Fail";
                            newDr["Remark"]       = "--";
                            resultData.Rows.Add(newDr);
                        }
                        continue;
                    }

                    try
                    {
                        // 取host name
                        hostName = string.Empty;
                        IPHostEntry host = Dns.GetHostEntry(ip);
                        hostName = host.HostName;
                        hashByIp.Add(ip.ToString(), hostName);
                    }
                    catch (SocketException)
                    {
                        hashByIp.Add(ip.ToString(), hostName);
                    }
                    newDr["NetworkIP"]    = ip.ToString();
                    newDr["PCName"]       = hostName;
                    newDr["IsSamePCName"] = "--";
                    newDr["Action"]       = "Success";
                    newDr["Remark"]       = "--";
                    resultData.Rows.Add(newDr);
                }
            }
            return(resultData);
        }