• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

使用C#得到局域网内所有主机名,IP地址,MAC地址,使用C#实现查看所有系统事件 ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
  this.lstViewEventDetail.Items.Clear();
string selectedItem = this.lstBoxCtrlEventType.SelectedItem.ToString().Trim();
EventLog[] eventLogs = EventLog.GetEventLogs();
foreach (EventLog evntLg in eventLogs)
{
if (selectedItem==evntLg.LogDisplayName)
{
selectedItem = evntLg.Log;
break;
}
}

if (EventLog.Exists(selectedItem))
{
EventLog selectedLog = new EventLog();
selectedLog.Log = selectedItem;
int i = 0;
foreach (EventLogEntry selectedEntry in selectedLog.Entries)
{
ListViewItem lvi = new ListViewItem();
lvi.SubItems.Clear();
lvi.SubItems[0].Text = selectedEntry.EntryType.ToString();
lvi.SubItems.Add(selectedEntry.TimeWritten.Date.ToString("yyyy-MM-dd"));
lvi.SubItems.Add(selectedEntry.TimeWritten.TimeOfDay.ToString());
lvi.SubItems.Add(selectedEntry.Source);
string category = selectedEntry.Category;
if (category=="(0)")
{
category = "NONE";
}
lvi.SubItems.Add(category);
lvi.SubItems.Add(selectedEntry.InstanceId.ToString());
string usrName = selectedEntry.UserName;
if (usrName==null)
{
usrName = "N/A";
}
lvi.SubItems.Add(usrName);
lvi.SubItems.Add(selectedEntry.MachineName);
this.lstViewEventDetail.Items.Add(lvi);
i++;
if (i > 300)
{
break;
}
}
//selectedLog.Clear();
}

 

 

 

 

C#得到局域网内所有主机名,IP地址,MAC地址

        private void btnCheck_Click(object sender, EventArgs e)
        {
            string ipAddress = this.cboBoxIp.Text.Trim();
            Regex rgx = new Regex(@"^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$");
            if (rgx.IsMatch(ipAddress))
            {
                this.labelIpResult.Text = GetName(ipAddress);
                this.labelMacResult.Text = GetMacAddress(ipAddress);
            }
            else
            {
                this.labelIpResult.Text = "IP address is not valid.";
                this.labelMacResult.Text = "No mac result";
            }
        }

        ///<summary>
        /// 得到所有局域网内机器名
        ///</summary>
        private void GetAllLocalMachineName()
        {
            System.Diagnostics.ProcessStartInfo prcssStrtInfo = new System.Diagnostics.ProcessStartInfo();
            prcssStrtInfo.FileName = @"C:\windows\system32\cmd.exe";
            prcssStrtInfo.Arguments = @"/c net view > F:\\LanMachineName.txt"; // 将所有局域网机器名输出到文本文件
            prcssStrtInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            System.Diagnostics.Process.Start(prcssStrtInfo);
        }

        ///<summary>
        /// 通过Ip地址获得主机名
        ///</summary>
        ///<param name="IpAddress"></param>
        ///<returns></returns>
        private string GetName(string IpAddress)//获取指定IP的计算机名称
        {
            string name="";
            try
            {
                IPHostEntry ipHstEntry = Dns.GetHostByAddress(IpAddress);//传递IP地址\
                //IPHostEntry ipHstEntry = Dns.GetHostEntry(IpAddress);
                name = ipHstEntry.HostName.ToString();//取得主机名
            }
            catch (Exception e)
            {
                name = e.Message;
            }
            return name;
        }

        ///<summary>
        /// 通过Ip得到MAC地址
        ///</summary>
        ///<param name="IpAddress"></param>
        ///<returns></returns>
        private string GetMacAddress(string IpAddress)//得到指定IP的MAC地址
        {
            string macAddress = "";
            Int32 ldest = 0;
            try
            {
                ldest = inet_addr(IpAddress);
            }
            catch (Exception iperr)
            {
                MessageBox.Show(iperr.Message);
            }
            Int64 macinfo = new Int64();
            Int32 len = 6;
            try
            {
                int res = SendARP(ldest, 0, ref macinfo, ref len);
            }
            catch (Exception err)
            {
                //    throw new Exception("在解析MAC地址过程发生了错误!"); 
                MessageBox.Show(err.Message);
            }
            string originalMACAddress = macinfo.ToString("X4");
            if (originalMACAddress != "0000" && originalMACAddress.Length == 12)
            { //合法MAC地址 
                string mac1, mac2, mac3, mac4, mac5, mac6;
                mac1 = originalMACAddress.Substring(10, 2);
                mac2 = originalMACAddress.Substring(8, 2);
                mac3 = originalMACAddress.Substring(6, 2);
                mac4 = originalMACAddress.Substring(4, 2);
                mac5 = originalMACAddress.Substring(2, 2);
                mac6 = originalMACAddress.Substring(0, 2);
                macAddress = mac1 + ":" + mac2 + ":" + mac3 + ":" + mac4 + ":" + mac5 + ":" + mac6;
                //canPing = true;
            }
            else
            {
                macAddress = "无法探测到MAC地址";
                //canPing = false;
            }
            return macAddress;
        }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
安装完vs2005后没有C#,VB.net,网站等模版的解决方法发布时间:2022-07-10
下一篇:
C#WPF简单自定义菜单切换动画发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap