Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on C# code to get the system up time of any remote machines with the following code.

C#
ConnectionOptions connOptions = new ConnectionOptions();
            connOptions.Impersonation = ImpersonationLevel.Impersonate;
            connOptions.EnablePrivileges = true;
            ManagementScope manScope = new ManagementScope(@"\\myshec79920d\ROOT\CIMV2", connOptions);
            manScope.Connect();
            ObjectGetOptions objectGetOptions = new ObjectGetOptions();
            ManagementPath managementPath = new ManagementPath("Win32_OperatingSystem");
            ManagementClass processClass = new ManagementClass(manScope, managementPath, objectGetOptions);
            foreach (ManagementObject item in processClass.GetInstances())
            {
                Console.WriteLine(item["SystemTime"]);
            }


In the above code what property name we can give as
C#
item["--"] 
to get the system up time(reboot time)

Please help me out to solve this. Thanks in advance
Posted

You can probably look it up yourself faster than I can type this in http://msdn.microsoft.com/en-us/library/aa394572(v=vs.85)[^] but if you're too busy try the LastBootUpTime property.

Alan.
 
Share this answer
 
Alternate solution:
Library needs to be inlcded is
C#
System.Management

C#
ConnectionOptions connOptions = new ConnectionOptions();
connOptions.Impersonation = ImpersonationLevel.Impersonate;
connOptions.EnablePrivileges = true;
ManagementScope manScope = new ManagementScope(string.Format(@"\\{0}\ROOT\CIMV2",serverName), connOptions);
manScope.Connect();
ObjectGetOptions objectGetOptions = new ObjectGetOptions();
ManagementPath managementPath = new ManagementPath("Win32_OperatingSystem");
ManagementClass processClass = new ManagementClass(manScope, managementPath, objectGetOptions);
foreach (ManagementObject item in processClass.GetInstances())
{
    Console.WriteLine(item["LastBootUpTime"].ToString());
}

For
C#
ConnectionOptions
we can provide Domain, User Name and password for secure connections.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900