How get list of local network computers?

前端 未结 6 450
甜味超标
甜味超标 2020-12-01 04:47

I am trying to get a list of local network computers. I tried to use NetServerEnum and WNetOpenEnum API, but both API return error code 6118

6条回答
  •  情深已故
    2020-12-01 05:12

    Solution with LINQ lambda syntax and unmanaged resource resolver

     public List GetNetworkHostNames()
     {
            using (var directoryEntry = new DirectoryEntry("WinNT:"))
            {
                return directoryEntry
                         .Children
                         .Cast()
                         .SelectMany(x=>x.Children.Cast())
                         .Where(c => c.SchemaClassName == "Computer")
                         .Select(c => c.Name)
                         .ToList();
            }
     }
    

提交回复
热议问题