我怎么能只收到我现在可以访问的计算机名称呢

本文关键字:访问 计算机名 怎么能 | 更新日期: 2023-09-27 18:27:06

Windows 7域网络。

我得到域名计算机名称:

List<String> compNames = new List<String>();
// If it isn't possible to be connected to a domain 
// network then we will receive an exception.
using (DirectoryEntry domainEntry = domain.GetDirectoryEntry())
using (DirectorySearcher searcher = new DirectorySearcher(
    domainEntry, "objectClass=computer", new[] { "Name" }))
using (SearchResultCollection resultCollection =
    searcher.FindAll()) {
    foreach (SearchResult searchResult in resultCollection) {
        compNames.Add(searchResult.Properties["Name"][0]
            .ToString());
    }
}

此代码还返回已不存在多年的计算机的名称和已关闭的计算机的姓名。如何仅接收此时可访问的计算机名称?有可能吗?

我怎么能只收到我现在可以访问的计算机名称呢

我建议您向域上的每台计算机ping检查其可访问性。

下面的功能你可以用来ping计算机:

        public String PingTest(string asMac)
        {
            String lsReturn = "";
            try
            {
                Ping pingreq = new Ping();
                PingReply rep = pingreq.Send(asMac);
                if (rep.Status == IPStatus.Success)
                {
                    lsReturn = "Success"
                    return lsReturn;
                }
                else
                {
                    lsReturn = "Error Pinging " + asMac + " " + rep.Status;
                }
            }
            catch (PingException pEx)
            {
                lsReturn = "PingException Error Pinging " + asMac + " " + pEx.Message + Environment.NewLine + pEx.StackTrace;
            }
            catch (NetworkInformationException nEx)
            {
                lsReturn = "NetworkInformationException Error Pinging " + asMac + " " + nEx.Message + Environment.NewLine + nEx.StackTrace;
            }
            catch (Exception ex)
            {
                lsReturn = "Error Pinging " + asMac + " " + ex.Message + Environment.NewLine + ex.StackTrace;
            }
            return lsReturn;
       }