Post by tzvikazHi,
I looked all over the web for a simple .NET application that uses .NET
class libraries to create a namespace, create class, add properties to
it and also add intances to this class.
I've seen an example using vbsript but I want to do it using
native .net code.
Can someone please help me with that?
Here is a small sample in C# (but I find this much easier in MOF, especially
when there are more than a few properties and qualifiers to create):
using System;
using System.Management;
namespace WMITest
{
class Program
{
static void Main()
{
// 1. Create a WMI namespace
// Connect to the parent namespace
// and get the WMI system class
// called __Namespace
ManagementClass namespaceClass =
new ManagementClass("Root", "__Namespace", null);
// Create an instance of __Namespace,
// assign its Name property
// and save it to the repository.
// This in fact creates a new WMI namespace.
ManagementObject namespaceInst =
namespaceClass.CreateInstance();
namespaceInst.Properties["Name"].Value = "Test";
namespaceInst.Put();
// 2. Connect to the new namespace
// and create a new class
// Create a ManagementClass instance
// and assign its __Class system property
ManagementClass newClass = new ManagementClass(
"Root\\Test", String.Empty, null);
newClass["__Class"] = "TestClass";
// Add a few properties
newClass.Properties.Add(
"Property1", CimType.String, false);
newClass.Properties["Property1"]
.Qualifiers.Add("Key", true);
newClass.Properties.Add(
"Property2", CimType.SInt32, false);
newClass.Put();
// 3. Create an instance of the new class
// and save it to the repository
ManagementObject newInstance =
newClass.CreateInstance();
newInstance["Property1"] = "KeyValue";
newInstance["Property2"] = 100;
newInstance.Put();
}
}
}
--
urkec
My blog:
http://theadminblog.blogspot.com/
My CodeProject articles:
http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid=4210975