[.NET C#].NET C#에서 WMI 네임스페이스 내 모든 WMI 클래스 이름 찾기

2023. 2. 2. 19:35이것저것

728x90

 

 

Finding all WMI class names within a WMI namespace with .NET C#

In this post we saw an example of using WMI objects such as ConnectionOptions, ObjectQuery and ManagementObjectSearcher to enumerate all local drives on a computer. Recall the SQL-like query we use…

dotnetcodr.com

 

이 게시물에서 우리는 ConnectionOptions, ObjectQuery 및 ManagementObjectSearcher와 같은 WMI 개체를 사용하여 컴퓨터의 모든 로컬 드라이브를 열거하는 예를 보았습니다. 

우리가 사용 했던 SQL과 쿼리가 유사하다는 것이 기억 날 것입니다:

ObjectQuery objectQuery = new ObjectQuery("SELECT Size, Name FROM Win32_LogicalDisk where DriveType=3");
 

이제 WMI 네임스페이스 내의 모든 WMI 클래스를 나열하는 기술을 볼 수 있을 것입니다. 

먼저 WMI 네임스페이스를 얻어 내야 합니다:

private static List<String> GetWmiNamespaces(string root)
{
    List<String> namespaces = new List<string>();
    try
    {
        ManagementClass nsClass = new ManagementClass(new ManagementScope(root), new ManagementPath("__namespace"), null);
        foreach (ManagementObject ns in nsClass.GetInstances())
        {
            string namespaceName = root + "\\" + ns["Name"].ToString();
            namespaces.Add(namespaceName);
            namespaces.AddRange(GetWmiNamespaces(namespaceName));
        }
    }
    catch (ManagementException me)
    {
        Console.WriteLine(me.Message);
    }
 
    return namespaces.OrderBy(s => s).ToList();
}
 

다음과 같이 이 메서드를 호출하여 로컬 컴퓨터의 모든 WMI 네임스페이스를 나열합니다:

List<String> namespaces = GetWmiNamespaces("root");
 

다음 메서드는 ManagementObjectSearcher 객체를 사용하여 WMI 네임스페이스에서 모든 클래스를 질의 검색하며 아래와 같습니다:

private static List<String> GetClassNamesWithinWmiNamespace(string wmiNamespaceName)
{
    List<String> classes = new List<string>();
    ManagementObjectSearcher searcher = new ManagementObjectSearcher
                (new ManagementScope(wmiNamespaceName),
                new WqlObjectQuery("SELECT * FROM meta_class"));
    List<string> classNames = new List<string>();
    ManagementObjectCollection objectCollection = searcher.Get();
    foreach (ManagementClass wmiClass in objectCollection)
    {
        string stringified = wmiClass.ToString();
        string[] parts = stringified.Split(new char[] { ':' });
        classes.Add(parts[1]);
    }
    return classes.OrderBy(s => s).ToList();
}
 
 

ManagementClass ToString 메서드는 콜론을 사용하여 클래스 이름을 네임스페이스에 연결하므로 Split 메서드가 필요 합니다.

그런 다음 각 네임스페이스 이름에 대해 이 메서드를 호출할 수 있습니다.

foreach (String namespaceName in namespaces)
{
    List<String> classNames = GetClassNamesWithinWmiNamespace(namespaceName);
}
 

모든 네임스페이스 내의 모든 클래스 이름을 나열하는 데는 많은 시간이 걸릴 수 있습니다.

 

여기에서 진단과 관련된 모든 게시물을 볼 수 있습니다.

 

https://dotnetcodr.com/diagnostics/

 

Diagnostics

Creating and deleting event logs with C# .NET Writing to the Windows Event Log with C# .NET Reading and clearing a Windows Event Log with C# .NET 4 ways to enumerate processes on Windows with C# .N…

dotnetcodr.com

이상.

728x90