无法评估函数调用超时

发布于 2024-07-29 06:46:23 字数 1511 浏览 1 评论 0原文

我用 C# 编写了一些基本代码,如下所示:

//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());
} 

其中提供用户名和密码作为我的本地桌面帐户的凭据。

当我到达从 ManagementObjectSearcher Get() 方法返回 ManagementObjectCollection 类型的行时,我收到一条错误(在运行时),指出当我尝试计算此行(foreach 循环之前的最后一行)时,函数调用超时。

没有例外,因此没有有关错误的更多详细信息。

我怎样才能解决这个问题? 代码对我来说看起来不错? 该代码位于另一台计算机上,因此我使用此处的代码(几乎遵循相同的步骤): http://www.csharphelp.com/archives2/archive334.html

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

九厘米的零° 2024-08-05 06:46:23

按原样运行代码时出现此异常:

System.Management.ManagementException 未处理
消息 =“用户凭据不能用于本地连接”
来源=“系统.管理”
...

它是在使用 oSearcher.Get() 的行上引发的,但 WMI 连接有问题,因为尝试连接到本地计算机。 如果您在 oMs 初始化后添加此行:

Console.WriteLine(oMs.Path.Path);

输出将如下所示(与之前相同的例外):

\.\MachineX

,因此您的路径将被解释为本地(点)计算机上名为 MachineX 的 WMI 命名空间。 这是由于 C# 转义规则 - 您可以将其用于 oMs 初始化(没有 oConn,因为凭据不能用于本地 WMI 连接):

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

并且您将在本地连接到 Root\Cimv2 命名空间。 始终使用完整的 WMI 路径是一个好习惯:

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

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);
苍白女子 2024-08-05 06:46:23

从您提供的链接参考中,我看到相同的 ManagementScope - \MachineX - 这不应该是您的本地计算机名称吗 - 与错误一致(由于找不到范围而超时)?

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)?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文