如何使用C#在网络上获取所有机器和用户 [英] How Can I Get All Machines And Users On A Network Using C#

查看:57
本文介绍了如何使用C#在网络上获取所有机器和用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道在网络上使用特定远程机器的计算机的名称,使用c#

Hi,I need to know name of the computer which is using a particular remote machine on network,using c#

推荐答案

我不知道它对你有用吗但是

您将获得使用System.Net.NetworkInformation的网络中系统的所有IP地址

i dont know is it useful for you but
you will get all the ip addresses of systems which are in network
using System.Net.NetworkInformation;
using System.Threading;
using System.Diagnostics;
using System.Collections.Generic;
using System;
 
namespace ConsoleApplication1
{
    class localipaddresses
    {
        private static List<ping> pingers = new List<ping>();
        private static int instances = 0;
 
        private static object @lock = new object();
 
        private static int result = 0;
        private static int timeOut = 250;
 
        private static int ttl = 5;
 
        public static void Main()
        {
            string baseIP = "192.168.0.";
 
            Console.WriteLine("Pinging 255 destinations of D-class in {0}*", baseIP);
 
            CreatePingers(255);
 
            PingOptions po = new PingOptions(ttl, true);
            System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
            byte[] data = enc.GetBytes("abababababababababababababababab");
 
            SpinWait wait = new SpinWait();
            int cnt = 1;
 
            Stopwatch watch = Stopwatch.StartNew();
 
            foreach (Ping p in pingers)
            {
                lock (@lock)
                {
                    instances += 1;
                }
 
                p.SendAsync(string.Concat(baseIP, cnt.ToString()), timeOut, data, po);
                cnt += 1;
            }
 
            while (instances > 0)
            {
                wait.SpinOnce();
            }
 
            watch.Stop();
 
            DestroyPingers();
 
            Console.WriteLine("Finished in {0}. Found {1} active IP-addresses.", watch.Elapsed.ToString(), result);
            Console.ReadKey();
 
        }
 
        public static void Ping_completed(object s, PingCompletedEventArgs e)
        {
            lock (@lock)
            {
                instances -= 1;
            }
 
            if (e.Reply.Status == IPStatus.Success)
            {
                Console.WriteLine(string.Concat("Active IP: ", e.Reply.Address.ToString()));
                result += 1;
            }
            else
            {
                //Console.WriteLine(String.Concat("Non-active IP: ", e.Reply.Address.ToString()))
            }
        }
 
        private static void CreatePingers(int cnt)
        {
            for (int i = 1; i <= cnt; i++)
            {
                Ping p = new Ping();
                p.PingCompleted += Ping_completed;
                pingers.Add(p);
            }
        }
 
        private static void DestroyPingers()
        {
            foreach (Ping p in pingers)
            {
                p.PingCompleted -= Ping_completed;
                p.Dispose();
            }
            pingers.Clear();
        }
    }
}
</ping></ping>


这篇关于如何使用C#在网络上获取所有机器和用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆