zoukankan      html  css  js  c++  java
  • [问题记录.dotnet]取网卡信息报错"找不到"-WMI

    异常:

    System.Management.ManagementException: 找不到 
       在 System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
       在 System.Management.ManagementObject.Initialize(Boolean getObject)
       在 System.Management.ManagementBaseObject.get_wbemObject()
       在 System.Management.ManagementBaseObject.get_ClassName()
       在 System.Management.ManagementClass.GetInstances(EnumerationOptions options)


    现象:

    某台机器之前使用正常,突然报这个错。
    其他机器均正常。


    解决:

    1. 确保服务“Windows Management Instrumentation”开启。
    2. 如果已经开启还是有问题,那么可能是WMI存储库损坏,需要重建WMI存储库。

    要重建WMI存储库,(Win7等系统)步骤操作:
    (如果是XP系统,参考 http://windowsxp.mvps.org/repairwmi.htm
    ————————————–

    1.开始->所有程序->附件,以管理员身份打开命令提示符command
    2.停止WMI服务:net stop winmgmt
    3.Repository目录改名备份:ren %windir%System32WbemRepository Repository_backup
    4.重启WMI服务:net start winmgmt
    5.运行 winmgmt /salvagerepository 尝试重建Repository
    6.注册WMI组件:
    cd /d %windir%system32wbem
    for /f %%s in (‘dir /b *.dll’) do regsvr32 /s %%s
    for /f %%s in (‘dir /b *.mof *.mfl’) do mofcomp %%s
    wmiprvse /regserver
    winmgmt /regserver

    7.重新启动系统

    8.重新安装自动更新成功
    —————————————–
    如果仍不成功,运行 %SystemRoot%System32WbemWbemTest.exe 测试WMI连接,点击连接,再次点击连接。如果仍然有连接错误,那么需要尝试全面的重建,运行下面的命令:

    rundll32.exe setupapi,InstallHinfSection WBEM 132 %windir%infwbemoc.inf

    执行上面的命令后,可能会需要读取 Windodws 7/Windows 2008 R2 安装盘。


    补充:重建WMI的批处理脚本

    @echo on 
    cd /d c: emp 
    if not exist %windir%system32wbem goto TryInstall 
    cd /d %windir%system32wbem 
    net stop winmgmt 
    winmgmt /kill 
    if exist Rep_bak rd Rep_bak /s /q 
    rename Repository Rep_bak 
    for %%i in (*.dll) do RegSvr32 -s %%i 
    for %%i in (*.exe) do call :FixSrv %%i 
    for %%i in (*.mof,*.mfl) do Mofcomp %%i 
    net start winmgmt 
    goto End 


    :FixSrv 
    if /I (%1) == (wbemcntl.exe) goto SkipSrv 
    if /I (%1) == (wbemtest.exe) goto SkipSrv 
    if /I (%1) == (mofcomp.exe) goto SkipSrv 
    %1 /RegServer 


    :SkipSrv 
    goto End 


    :TryInstall 
    if not exist wmicore.exe goto End 
    wmicore /s 
    net start winmgmt 
    :End 


    将这段代码保存成  BAT格式的后  在服务器上运行 即可




    分析:

    导致错误的代码段
    public static string GetNetworkCardInfo()
    { 
        ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
        ManagementObjectCollection moc2 = mc.GetInstances();  <span style="color:#ff0000;">//这一句抛出的异常</span>
        string netcard = string.Empty;
        foreach (ManagementObject mo in moc2)
        {
            if ((bool)mo["IPEnabled"] == true)
            {
                netcard = mo["MacAddress"].ToString();
                mo.Dispose();
                break;
            }
            mo.Dispose();
        }
        return netcard;
    }
    原因:
    System.Management 命名空间下的方式基本都是依赖WMI (Windows Management Instrumentation) 服务的,所以自然就联想到WMI的问题。然后去用工具或vbs脚本检验一下,基本就能确认了。
     

  • 相关阅读:
    Java 并发工具包 java.util.concurrent 用户指南
    Java 序列化Serializable详解(附详细例子)
    Spring之FactoryBean .
    《用chsh选择shell》-linux命令五分钟系列之十二
    《vi中的替换艺术》-linux命令五分钟系列之十一
    0-1背包问题
    Java关键字final、static使用总结
    《作业控制系列》-“linux命令五分钟系列”之十
    《zip命令》-linux命令五分钟系列之九
    《bunzip2命令》-linux命令五分钟系列之八
  • 原文地址:https://www.cnblogs.com/fj365/p/13295463.html
Copyright © 2011-2022 走看看