In trying to find a set of computers on a network, I discovered the ManagementObjectSearcher class along with the ManagementObject class. The constructor for ManagementObjectSearcher takes a sql/sql-esque query as a parameter. Currently, I'm using the query I found in our legacy code:
ManagementObjectSearcher adapters = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapter");
But I'm trying to figure out how to know what could possibly be in the query. What are all the tables/objects? What are all the possible constraints? What general topic would this class and related questions fall under so I could look this up and learn more about it?
Upon further research it appears as though this is part of WMI (Windows Management Infrastructure)
WMI Queries: http://msdn.microsoft.com/en-us/library/ms186146(v=vs.80).aspx
List of WMI search criteria: http://msdn.microsoft.com/en-us/library/windows/desktop/aa394554(v=vs.85).aspx
Using WMI: http://msdn.microsoft.com/en-us/library/windows/desktop/aa393964(v=vs.85).aspx
Books: http://www.amazon.com/s/ref=nb_sb_noss_1?url=search-alias%3Daps&field-keywords=WMI
Sample Code:
ManagementObjectSearcher adapters = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionStatus = 2");
foreach (ManagementObject item in adapters.Get())
{
foreach (ManagementObject setting in item.GetRelated("Win32_NetworkAdapterConfiguration"))
{
if ((!setting["IPAddress"].IsNull()) &&
(!setting["IPSubnet"].IsNull()))
{
foreach (string value in (String[])setting["IPAddress"])
{
mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
mySocket.Bind(new IPEndPoint(IPAddress.Parse(value), 49984));
//etc...
}
}
}
}
It looks like this is a rather monumental topic on the whole, but at least this has given me a place to start trying to understand it. However, I'm still completely unsure as to whether or not this is a good solution for the problem I'm trying to solve.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With