rgshankar
asked on
C# - System.Management.ManagementObject
I have an ManagementObject item, it has a count of 60.
I am doing something like this
foreach (ProperttData prop in item.properties)
{...
...}
prop.value returns a System.Array value. How do I assign this to a string variable.
prop.value is an object, how do I assign this to a string.
I am doing something like this
foreach (ProperttData prop in item.properties)
{...
...}
prop.value returns a System.Array value. How do I assign this to a string variable.
prop.value is an object, how do I assign this to a string.
ASKER
I am doing the same way, I am using Win32_NetworkAdapterConfig uration to get the IPAddress which is an array.
When I do
string DMTF= item.Properties["IPAddress "].Value.T oString(); it returns
DMTF = "System.String[]" and not the actual IP address 000.000.000.000
item.Properties["IPAddress "].Value has 0.0.0.0
Any idea...
When I do
string DMTF= item.Properties["IPAddress
DMTF = "System.String[]" and not the actual IP address 000.000.000.000
item.Properties["IPAddress
Any idea...
how about this
// Create an instance of IPAddress for the specified address string (in
// dotted-quad, or colon-hexadecimal notation).
IPAddress address = IPAddress.Parse(ipAddress) ;
// Display the address in standard notation.
Console.WriteLine("Parsing your input string: " + "\"" + ipAddress + "\"" + " produces this address (shown in its standard notation): "+ address.ToString());
// Create an instance of IPAddress for the specified address string (in
// dotted-quad, or colon-hexadecimal notation).
IPAddress address = IPAddress.Parse(ipAddress)
// Display the address in standard notation.
Console.WriteLine("Parsing
string DMTF= item.Properties["IPAddress "].Value.T oString(); it returns
IPAddress address = IPAddress.Parse(ipAddress) ;
IPAddress address = IPAddress.Parse(ipAddress)
Ok this should help..If not hopefully someone else can get you on the right path..Good Luck
// using System.Management;
public void UseWMI()
{
string query = "SELECT * FROM Win32_NetworkAdapterConfig uration"
+ " WHERE IPEnabled = 'TRUE'";
ManagementObjectSearcher moSearch = new ManagementObjectSearcher(q uery);
ManagementObjectCollection moCollection = moSearch.Get();
// Every record in this collection is a network interface
foreach(ManagementObject mo in moCollection)
{
Console.WriteLine("HostNam e = " + mo["DNSHostName"]);
Console.WriteLine("Descrip tion = " + mo["Description"]);
// IPAddresses, probably have more than one value
string[] addresses = (string[])mo["IPAddress"];
foreach(string ipaddress in addresses)
{
Console.WriteLine("IPAddre ss = " + ipaddress);
}
// IPSubnets, probably have more than one value
string[] subnets = (string[])mo["IPSubnet"];
foreach(string ipsubnet in subnets)
{
Console.WriteLine("IPSubne t = " + ipsubnet);
}
// DefaultIPGateways, probably have more than one value
string[] defaultgateways = (string[])mo["DefaultIPGat eway"];
foreach(string defaultipgateway in defaultgateways)
{
Console.WriteLine("Default IPGateway = " + defaultipgateway);
}
}
}
// using System.Management;
public void UseWMI()
{
string query = "SELECT * FROM Win32_NetworkAdapterConfig
+ " WHERE IPEnabled = 'TRUE'";
ManagementObjectSearcher moSearch = new ManagementObjectSearcher(q
ManagementObjectCollection
// Every record in this collection is a network interface
foreach(ManagementObject mo in moCollection)
{
Console.WriteLine("HostNam
Console.WriteLine("Descrip
// IPAddresses, probably have more than one value
string[] addresses = (string[])mo["IPAddress"];
foreach(string ipaddress in addresses)
{
Console.WriteLine("IPAddre
}
// IPSubnets, probably have more than one value
string[] subnets = (string[])mo["IPSubnet"];
foreach(string ipsubnet in subnets)
{
Console.WriteLine("IPSubne
}
// DefaultIPGateways, probably have more than one value
string[] defaultgateways = (string[])mo["DefaultIPGat
foreach(string defaultipgateway in defaultgateways)
{
Console.WriteLine("Default
}
}
}
ASKER
No...
string DMTF= item.Properties["IPAddress "].Value.T oString();
will return string "System.String[]".
It is not a valid IP, when you try to parse it, give an error.
IPAddress address = IPAddress.Parse(DMFT); - will return error becaue DMFT = "System.String[]" not 0.0.0.0
string DMTF= item.Properties["IPAddress
will return string "System.String[]".
It is not a valid IP, when you try to parse it, give an error.
IPAddress address = IPAddress.Parse(DMFT); - will return error becaue DMFT = "System.String[]" not 0.0.0.0
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks. I think I got it
try
{
string ConfigNamespace = @"\\.\root\cimv2";
string query = "select * from Win32_OperatingSystem";
ManagementObjectSearcher searcher = new ManagementObjectSearcher( ConfigNamespace, query );
ManagementObjectCollection
foreach (ManagementObject item in collection)
{
string DMTF= item.Properties["LastBootU
DateTime bootup = ManagementDateTimeConverte
MessageBox.Show(bootup.ToS
System.Diagnostics.EventLo
}
catch (Exception e)
{
System.Diagnostics.EventLo
throw new Exception("Failed to get last reboot", e);
}
**************************
Another example!!
string ConfigNamespace = "\\\\.\\root\\cimv2";
string query = "select * from Win32_Processor";
ManagementObjectSearcher searcher = new
ManagementObjectSearcher(C
ManagementObjectCollection
foreach (ManagementObject item in collection)
{
foreach (PropertyData property in item.Properties)
{
try
{
Console.WriteLn(property.N
property.Value.ToString())
}
catch (Exception)
{
}
}
}
I hope this helps or gets you pointed in the right direction