MMC Action Sample
This Code eample will help to add Child Node and Root Node programatically. This article does not contain the basic step to create MMC. This code allows to do action - Add Child Node and Add to Root Node.
MmcListView has actions Refresh, SortByName and SortByValue. The last two are the ModeActions. I have created 4 different .cs classes in this examples and each one of these has its own specific funationalities.
- HelloActionSampleClass.cs
- InstallHelloActionInstaller.cs
- HelloActionScopeNode.cs
- ActionListView.cs
We start the example by creating a C# class library.
HelloActionSampleClass class - is the starting class. This provides the main entry point for the creation of a snap-in. This class is derived from the SnapIn class which is in Microsoft.ManagementConsole namespace.
[SnapInSettings("{43E241A2-2CC9-4452-8F54-C466613D1F1F}",
DisplayName = "Hello Action SnapIn",
Description = "Gigy's Hello Action SnapIn")]
public class HelloActionSampleClass : SnapIn
{
///
/// Initializes a new instance of the HelloActionSampleClass class.
///
public HelloActionSampleClass()
{
// Create the root node
this.RootNode = new HelloActionScopeNode();
this.RootNode.DisplayName = "HelloWorld Actions Sample";
this.RootNode.ImageIndex = 0;
// Create a message view for the root node.
MmcListViewDescription lvd = new MmcListViewDescription();
lvd.DisplayName = "HelloWorld (MmcListView)";
lvd.ViewType = typeof(ActionListView);
lvd.Options = MmcListViewOptions.ExcludeScopeNodes | MmcListViewOptions.AllowUserInitiatedModeChanges;
// Attach the view to the root node
this.RootNode.ViewDescriptions.Add(lvd);
this.RootNode.ViewDescriptions.DefaultIndex = 0;
}
}
In the constructor, we create a custom RootNode called HelloActionScopeNode. We also create a message view for the root node. This will get dispalyed on content pane when the RootNode is selected.
InstallHelloActionInstaller class - is used for installing the Snap-In. It allows the .Net framework InstallUtil.exe to install the assembly.
[RunInstaller(true)]
public class InstallUtilSupport : SnapInInstaller
{
}
HelloActionScopeNode class - has the implementation of the custom ScopeNode.
public HelloActionScopeNode()
{
// add actions
this.ActionsPaneItems.Add(new Microsoft.ManagementConsole.Action("Add Child",
"Adds a new scope node under this node", 0, "AddChild"));
this.ActionsPaneItems.Add(new Microsoft.ManagementConsole.Action("Add to Root",
"Adds a new scope node to the root", 0, "AddToRoot"));
}
protected override void OnAction(Microsoft.ManagementConsole.Action action, AsyncStatus status)
{
switch ((string)action.Tag)
{
case "AddChild":
{
HelloActionScopeNode helloactionScopeNode = new HelloActionScopeNode();
helloactionScopeNode.DisplayName = "JustIn " + System.DateTime.Now.ToLongTimeString();
helloactionScopeNode.ImageIndex = 0;
this.Children.Add(helloactionScopeNode);
break;
}
case "AddToRoot":
{
HelloActionScopeNode HelloActionScopeNode = new HelloActionScopeNode();
HelloActionScopeNode.DisplayName = "JustIn " + System.DateTime.Now.ToLongTimeString();
HelloActionScopeNode.ImageIndex = 0;
((HelloActionSampleClass)this.SnapIn).RootNode.Children.Add(HelloActionScopeNode);
break;
}
}
}
In the constructor, we add two Actions to the ActionsPaneItems list. One for adding Child Node and the other one for adding Node to the Root. I believe the code is self explanatory.
ActionListView class - is derived from MmcListView class. This is a basic result pane that lists ResultNodes.
Once all the code is in place and a sucess build, run the installer utility
Open the Management Console, add the Snap-In to the list.
This is the screen shot of the IDE
