尝试删除共享时出现 InvalidOperationException

发布于 2024-12-26 03:17:58 字数 803 浏览 0 评论 0原文

我正在尝试删除共享,但它不断在“InvokeMethod”行上抛出 InvalidOperationException。我是 WMI 新手,我不知道我的代码中是否缺少某些内容...您能帮助我吗?

ManagementScope ms = new ManagementScope(@"\\localhost\root\cimv2");

ManagementObjectSearcher searcher = 
    new ManagementObjectSearcher(
        "Select * from Win32_Share where Name ='RanorexTests'");

ManagementObjectCollection result = searcher.Get();

((ManagementObject)result.GetEnumerator().Current).InvokeMethod(
    "Delete", new object[] { });

编辑(完整堆栈跟踪):

在 System.Management.ManagementObjectCollection.ManagementObjectEnumerator.get_Current() 在 RanorexTests.CalculatorUnitTest.deleteShare() 中 C:\RanorexSolution\RanorexTests\RanorexTests\FlashCalculator\CalculatorUnitTest.cs:line 126

谨致的问候, 穆拉斯曼

I'm trying to delete a share but it keeps throwing InvalidOperationException on the "InvokeMethod" line. I'm new to WMI and I don't know if I'm missing something on my code... Can you help me please?

ManagementScope ms = new ManagementScope(@"\\localhost\root\cimv2");

ManagementObjectSearcher searcher = 
    new ManagementObjectSearcher(
        "Select * from Win32_Share where Name ='RanorexTests'");

ManagementObjectCollection result = searcher.Get();

((ManagementObject)result.GetEnumerator().Current).InvokeMethod(
    "Delete", new object[] { });

EDIT (full stacktrace):

at
System.Management.ManagementObjectCollection.ManagementObjectEnumerator.get_Current()
at RanorexTests.CalculatorUnitTest.deleteShare() in
C:\RanorexSolution\RanorexTests\RanorexTests\FlashCalculator\CalculatorUnitTest.cs:line
126

Best regards,
Mourasman

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

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

发布评论

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

评论(1

说不完的你爱 2025-01-02 03:17:58

枚举器以 Current 为空开始。你必须先调用MoveNext()。

来自 http://msdn.microsoft.com/en-us /library/system.collections.ienumerator.aspx

C# 语言的 foreach 语句(Visual Basic 中的 foreach)隐藏了枚举器的复杂性。因此,建议使用 foreach 而不是直接操作枚举器。

最初,枚举数位于集合中第一个元素之前。 Reset 方法还将枚举器带回该位置。在此位置,调用 Current 属性会引发异常。因此,在读取 Current 的值之前,必须调用 MoveNext 方法将枚举数前进到集合的第一个元素。

即使我知道集合中只有一项,我通常也会将其放入 foreach 循环中:

foreach(ManagementObject obj in result)
  obj.InvokeMethod("Delete", new object[] { });

Enumerators start with Current being null. You have to MoveNext() first.

From http://msdn.microsoft.com/en-us/library/system.collections.ienumerator.aspx:

The foreach statement of the C# language (for each in Visual Basic) hides the complexity of the enumerators. Therefore, using foreach is recommended instead of directly manipulating the enumerator.

Initially, the enumerator is positioned before the first element in the collection. The Reset method also brings the enumerator back to this position. At this position, calling the Current property throws an exception. Therefore, you must call the MoveNext method to advance the enumerator to the first element of the collection before reading the value of Current.

I usually just throw mine into a foreach loop even if I know there's only going to be one item in the collection:

foreach(ManagementObject obj in result)
  obj.InvokeMethod("Delete", new object[] { });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文