检查WMI命名空间从C#中存在 [英] check if WMI namespace exists from c#

查看:434
本文介绍了检查WMI命名空间从C#中存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要检查是否安装了特定的机器上的某些功能。
我有检查这个PowerShell代码,现在我想从.NET代码检查。
我可以看到,在该cmdlet,代码检查,如果有一个无效的命名空间错误。



在网上搜索,我发现下面的代码:

  ManagementClass MyClass的=新ManagementClass(范围,路径,getOptions); 


{
myClass.get();
}
赶上(System.Management.Exception前)
{
如果(ex.ErrorCode == ManagementStatus.InvalidNamespace)
{
返回真;
}
}
...



我要清理这段代码一点,所以基本上我有2个问题:




  1. 有另一种方式来检查的 InvalidNamespace 错误? (我复制的代码后来被调用在 MyClass的一些方法,所以我不知道如果我能以某种方式实现一个更直接的方式我的目标)


  2. 我真的需要参数 getOptions



解决方案

要得到所有的WMI命名空间,您必须首先连接到根命名空间,然后查询所有__NAMESPACE实例,每个实例递归重复此过程。关于getOptions参数,它是一个 ObjectGetOptions 类不在此情况下有必要,这样可以为null。



勾选此代码来获取所有的WMI命名空间(可以填充列表与信息,然后检查是否命名空间中的机器存在)

 使用系统; 
使用System.Collections.Generic;
使用System.Text;
使用System.Management;

命名MyConsoleApplication
{
类节目
{
静态私人无效GetWmiNameSpaces(串根)
{

{
ManagementClass nsClass =新ManagementClass(新管理范围(根),新ManagementPath(__命名空间),NULL);
的foreach(在nsClass.GetInstances的ManagementObject NS())
{
串namespaceName =根+\\+ NS [名称]的ToString()。
Console.WriteLine(namespaceName);
//调用funcion递归
GetWmiNameSpaces(namespaceName);
}
}
赶上(ManagementException E)
{
Console.WriteLine(e.Message);
}
}


静态无效的主要(字串[] args)
{
//设置初始根搜索
GetWmiNameSpaces(根);
Console.ReadKey();
}
}
}


I want to check if a certain feature is installed on a certain machine. I have a powershell code that checks this, and now I want to check this from .net code. I can see that in the cmdlet, the code checks if there is an invalid namespace error.

When searching the web, I found the following code:

ManagementClass myClass = new ManagementClass(scope, path, getOptions);

try
{
    myClass.get();
}
catch (System.Management.Exception ex)
{
    if (ex.ErrorCode == ManagementStatus.InvalidNamespace)
    {
         return true;
    }
}
 ...   

I want to clean this code a bit, so basically I have 2 questions:

  1. Is there another way to check for an InvalidNamespace error? (The code I've copied was later used to invoke some method within myClass, so I wonder if I can somehow achieve my goal in a more direct way)

  2. Do I really need the parameter getOptions?

解决方案

To get all the wmi namespaces, you must first connect to the root namespace and then query for all the __NAMESPACE instances, and for each instance recursively repeat this process. about the getOptions parameter which is a ObjectGetOptions class is not necessary in this case, so can be null.

Check this code to get all the wmi namespaces (you can populate a list with that info and then check if the namespace exist in the machine)

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;

namespace MyConsoleApplication
{
    class Program
    {
        static private void GetWmiNameSpaces(string root)
        {
            try
            {
                ManagementClass nsClass = new ManagementClass( new ManagementScope(root), new ManagementPath("__namespace"), null);
                foreach (ManagementObject ns in nsClass.GetInstances())
                {
                    string namespaceName = root + "\\" + ns["Name"].ToString();
                    Console.WriteLine(namespaceName);
                    //call the funcion recursively                               
                    GetWmiNameSpaces(namespaceName);
                }
            }
            catch (ManagementException e)
            {
                Console.WriteLine(e.Message);
            }
        }


        static void Main(string[] args)
        {
            //set the initial root to search
            GetWmiNameSpaces("root");
            Console.ReadKey();
        }
    }
}

这篇关于检查WMI命名空间从C#中存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆