简体   繁体   English

如何删除 WMI 类

[英]How to delete WMI class

I have been asked to create an application to manage instances in the WMI.我被要求创建一个应用程序来管理 WMI 中的实例。

In previous version of this application the WMI class looks like this在此应用程序的先前版本中,WMI 类如下所示

{String Key,String Data}

for the new version the WMI class looks like this对于新版本,WMI 类看起来像这样

{String Key,String Data1,String Data2} . {String Key,String Data1,String Data2}

Now I'm facing a problem if the WMI class does exist but not in the new format.现在,如果 WMI 类确实存在但不是新格式,我将面临一个问题。 This might happen for upgrade purposes.这可能出于升级目的而发生。

I want to delete the old WMI class and create new one with the new format.我想删除旧的 WMI 类并使用新格式创建新的 WMI 类。

How I can do this?我怎么能做到这一点?

you can use System.Management your code sould look like this:你可以使用 System.Management 你的代码看起来像这样:

    void DeleteClass()
    {
        string ns, cl;
        ns = "nameSpace path";
        cl = "ClassName";
        string serverString = @"\\" + System.Environment.GetEnvironmentVariable("COMPUTERNAME") +ns;
        ManagementScope mScope = new ManagementScope(serverString);
        using (ManagementClass cls = new ManagementClass(mScope, new ManagementPath(cl), new ObjectGetOptions()))
        {
            using (ManagementObjectCollection objCol = cls.GetInstances())
            {
                foreach (ManagementObject obj in objCol)
                {
                    obj.Delete();//cleare the instances
                }
            }
            cls.Delete();//delete the class
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM