• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5391
  • Last Modified:

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.
0
rgshankar
Asked:
rgshankar
  • 5
  • 3
1 Solution
 
NewMom2BrandonCommented:
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
0
 
rgshankarAuthor Commented:
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...
0
 
NewMom2BrandonCommented:
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());

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
NewMom2BrandonCommented:
string DMTF= item.Properties["IPAddress"].Value.ToString(); it returns
IPAddress address = IPAddress.Parse(ipAddress);
0
 
NewMom2BrandonCommented:
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);
      }
   }
}
0
 
rgshankarAuthor Commented:
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

0
 
NewMom2BrandonCommented:
I think what I just posted... should help

this line at least

string[] addresses = (string[])mo["IPAddress"];

I am curious why you are not using WMI instead?
0
 
rgshankarAuthor Commented:
Thanks. I think I got it
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now