tags:

views:

256

answers:

2

Hi,

I wrote some basic code in C# like so:

//Connection credentials to the remote computer - not needed if the logged in account has access
ConnectionOptions oConn = new ConnectionOptions();
oConn.Username = "";
oConn.Password = "";

System.Management.ManagementScope oMs = new System.Management.ManagementScope("\\MachineX", oConn);    

//get Fixed disk stats
System.Management.ObjectQuery oQuery = new System.Management.ObjectQuery("select FreeSpace,Size,Name from Win32_LogicalDisk where DriveType=3");

//Execute the query 
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs,oQuery);

//Get the results
ManagementObjectCollection oReturnCollection = oSearcher.Get();   

//loop through found drives and write out info
foreach( ManagementObject oReturn in oReturnCollection )
{
    // Disk name
    Console.WriteLine("Name : " + oReturn["Name"].ToString());
    // Free Space in bytes
    Console.WriteLine("FreeSpace: " + oReturn["FreeSpace"].ToString());
    // Size in bytes
    Console.WriteLine("Size: " + oReturn["Size"].ToString());
}

Where the username and password are provided as the credentials for my local desktop account.

When I get to the line where the ManagementObjectCollection type is returned from the ManagementObjectSearcher Get() method, I get an error (at runtime) saying function call timed out when I try to evaluate this line (last line before the foreach loop).

There is no exception so no more details on the error.

How can I fix this? The code looks fine to me? This code is on another machine so I am using the code from here (pretty much follows the same steps): http://www.csharphelp.com/archives2/archive334.html

Thanks

A: 

From the link reference you gave, I see the same ManagementScope - \MachineX - shouldn't this be your local machine name instead - would be consistent with the error (time out due to not finding the scope)?

S.Skov
Its probably safe to assume the user has posted "\MachineX" for confidentiality purpose, they will no doubt be replacing that with their actual machine name...
James
I did actually change the name of the machine to the appropriate name of the machine I am coding on, but no luck. :( The code is the same, all the parameters specific to my PC have changed.
dotnetdev
+1  A: 

I get this exception when running your code as it is:

System.Management.ManagementException was unhandled Message="User credentials cannot be used for local connections " Source="System.Management" ...

It is raised on the line where oSearcher.Get() is used, but the problem WMI connection, because an attempt is made to connect to local computer. If you add this line after oMs initialization:

Console.WriteLine(oMs.Path.Path);

the output will be like this (with the same exception as before):

\.\MachineX

so your path is interpreted as a WMI namespace named MachineX on the local (dot) machine. This is due to C# escaping rules - you could use this for oMs initialization (without oConn, because credentials can't be used for local WMI connections):

System.Management.ManagementScope oMs = 
new System.Management.ManagementScope("\\root\\cimv2");

and you would be connected to Root\Cimv2 namespace locally. It is a good practice to always use full WMI paths:

System.Management.ManagementScope oMs = 
new System.Management.ManagementScope(@"\\MachineX\root\cimv2", oConn);
Uros Calakovic