Link to home
Start Free TrialLog in
Avatar of rgshankar
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.
Avatar of NewMom2Brandon
NewMom2Brandon

I am pretty new to C# but I have been doing a search the same way. Here is what helped me. I also included the code I used for the shutdown

try
{
string ConfigNamespace = @"\\.\root\cimv2";
string query = "select * from Win32_OperatingSystem";
                        
ManagementObjectSearcher searcher = new ManagementObjectSearcher( ConfigNamespace, query );
                                                      
ManagementObjectCollection collection = searcher.Get();
                                                                  
foreach (ManagementObject item in collection)
{
   string DMTF= item.Properties["LastBootUpTime"].Value.ToString();
   DateTime bootup = ManagementDateTimeConverter.ToDateTime(DMTF).Date;
                                                      
MessageBox.Show(bootup.ToString("dd-MMM-yyyy"),"Shutdown Time", MessageBoxButtons.OK,MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,MessageBoxOptions.ServiceNotification);
System.Diagnostics.EventLog.WriteEntry(bootup.ToString("dd-MMM-yyyy"), "Last booted up");            
}
                        
catch (Exception e)
{
   System.Diagnostics.EventLog.WriteEntry(this.ToString(),e.ToString());
   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(ConfigNamespace, query);    
       
ManagementObjectCollection collection = searcher.Get();

foreach (ManagementObject item in collection)
{
foreach (PropertyData property in item.Properties)
{
try
{
    Console.WriteLn(property.Name + ":" +
property.Value.ToString());
}
catch (Exception)
{
}
}
}      

I hope this helps or gets you pointed in the right direction
Avatar of rgshankar

ASKER

I am doing the same way, I am using Win32_NetworkAdapterConfiguration to get the IPAddress which is an array.

When I do
string DMTF= item.Properties["IPAddress"].Value.ToString(); 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...
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());

string DMTF= item.Properties["IPAddress"].Value.ToString(); it returns
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_NetworkAdapterConfiguration"
        + " WHERE IPEnabled = 'TRUE'";
   ManagementObjectSearcher moSearch = new ManagementObjectSearcher(query);
   ManagementObjectCollection moCollection = moSearch.Get();
   
   // Every record in this collection is a network interface
   foreach(ManagementObject mo in moCollection)
   {
      Console.WriteLine("HostName = " + mo["DNSHostName"]);
      Console.WriteLine("Description = " + mo["Description"]);
     
      // IPAddresses, probably have more than one value
      string[] addresses = (string[])mo["IPAddress"];
      foreach(string ipaddress in addresses)
      {
         Console.WriteLine("IPAddress = " + ipaddress);
      }
     
      // IPSubnets, probably have more than one value
      string[] subnets = (string[])mo["IPSubnet"];
      foreach(string ipsubnet in subnets)
      {
         Console.WriteLine("IPSubnet = " + ipsubnet);
      }
     
      // DefaultIPGateways, probably have more than one value
      string[] defaultgateways = (string[])mo["DefaultIPGateway"];
      foreach(string defaultipgateway in defaultgateways)
      {
         Console.WriteLine("DefaultIPGateway = " + defaultipgateway);
      }
   }
}
No...

string DMTF= item.Properties["IPAddress"].Value.ToString();
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
Avatar of NewMom2Brandon
NewMom2Brandon

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks. I think I got it