追加するファイル
[1] クラスライブラリ : スナップイン
⇒今回は、ファイル名「SampleSnapIn.cs」とする
[2] クラスライブラリ : MMCのロジカルなView
⇒今回は、ファイル名「ProcessView.cs」とする
[3] ユーザコントロール : WinFormのコントロールView
⇒今回は、ファイル名「SampleProcessControl.cs」とする
※デザインは、以下の通り。
* SplitContainer x 1
+ ListView x 1
+ Label x 1
+ TextBox x 2
サンプル
SampleSnapIn.cs
using System.ComponentModel;
using Microsoft.ManagementConsole;
[SnapInSettings("{3D8302F1-E86A-430f-9B16-B294323CEBB7}", // GUIDの作成で取得したIDを記入
DisplayName = "Proccess Viewer Sample", // ここが、MMCに追加する時のスナップイン名になる
Vendor = "Sample Company", // ここが、MMCに追加する時のベンダー名になる
Description = "This is a sample to display process lists in MMC.")]
public class SampleSnapIn : SnapIn
{
public SampleSnapIn()
{
this.RootNode = new ScopeNode();
this.RootNode.DisplayName = "Process Viewer";
FormViewDescription fvd = new FormViewDescription();
fvd.ControlType = typeof(ProcessView);
fvd.ViewType = typeof(SampleProcessControl);
fvd.DisplayName = "プロセス一覧";
this.RootNode.ViewDescriptions.Add(fvd);
this.RootNode.ViewDescriptions.DefaultIndex = 0;
}
}
[RunInstaller(true)]
public class InstallUtilSupport : SnapInInstaller
{
}
ProcessView.cs
using System.Collections.ObjectModel;
using System.Management.Automation;
using Microsoft.ManagementConsole;
using Microsoft.ManagementConsole.Advanced;
class ProcessView : FormView
{
protected override void OnInitialize(AsyncStatus status)
{
base.OnInitialize(status);
RunspaceInvoke invoker = new RunspaceInvoke();
Collection<PSObject> processes = invoker.Invoke("Get-Process");
SampleProcessControl processControl = (SampleProcessControl)this.Control;
processControl.RefreshData(processes);
}
protected override void OnSelectionAction(Action action, AsyncStatus status)
{
switch ((string)action.Tag)
{
case "KillProcess":
MessageBoxParameters msgParm = new MessageBoxParameters();
msgParm.Caption = "サンプル";
msgParm.Buttons = System.Windows.Forms.MessageBoxButtons.OK;
msgParm.Text = "サンプルプログラムです";
this.SnapIn.Console.ShowDialog(msgParm);
System.Threading.Thread.Sleep(1000);
break;
}
}
}
SampleProcessControl.cs
using System;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Windows.Forms;
using Microsoft.ManagementConsole;
public partial class SampleProcessControl : UserControl, IFormViewControl
{
public SampleProcessControl()
{
InitializeComponent();
}
#region IFormViewControl メンバ
private ProcessView proccessView;
#endregion
public void Initialize(FormView view)
{
this.proccessView = (ProcessView)view;
this.proccessView.SelectionData.ActionsPaneItems.Clear();
this.proccessView.SelectionData.ActionsPaneItems.Add(
new Microsoft.ManagementConsole.Action("Kill Process", "kill the process", -1, "KillProcess"));
}
internal void RefreshData(Collection<PSObject> processes)
{
foreach (PSObject process in processes)
{
System.Diagnostics.Process processObjcet =
(System.Diagnostics.Process)process.BaseObject;
ListViewItem listViewItem = new ListViewItem();
listViewItem.Text = processObjcet.Id.ToString();
listViewItem.SubItems.Add(processObjcet.ProcessName);
this.listView1.Items.Add(listViewItem);
}
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.listView1.SelectedItems.Count == 0)
{
this.proccessView.SelectionData.Clear();
}
else
{
this.textBox1.Text =
this.listView1.SelectedItems[0].Text;
this.textBox2.Text =
this.listView1.SelectedItems[0].SubItems[1].Text;
this.proccessView.SelectionData.Update(
this.listView1.SelectedItems[0].Text, false, null, null);
this.proccessView.SelectionData.DisplayName =
this.listView1.SelectedItems[0].Text;
}
}
}