尝试删除共享时出现 InvalidOperationException
我正在尝试删除共享,但它不断在“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
枚举器以 Current 为空开始。你必须先调用MoveNext()。
来自 http://msdn.microsoft.com/en-us /library/system.collections.ienumerator.aspx:
即使我知道集合中只有一项,我通常也会将其放入 foreach 循环中:
Enumerators start with Current being null. You have to MoveNext() first.
From http://msdn.microsoft.com/en-us/library/system.collections.ienumerator.aspx:
I usually just throw mine into a foreach loop even if I know there's only going to be one item in the collection: