Windows Management Instrumentation. Опрос свойств, методов и описателей. Разработка компонента WMIControl, страница 39

listView.Sorting = SortOrder.Ascending;

listComparer = new ProcessListComparer();

listView.ListViewItemSorter = listComparer;

}

void GetSysInfo()

{

if (!rbCurrent.Checked)

{

sMachine = txtName.Text.Trim();

if (string.IsNullOrEmpty(sMachine))

{

MessageBox.Show("Machine IP address or name is needed");

return;

}

co = new ConnectionOptions();

co.Username = txtUserID.Text.Trim();

co.Password = txtPass.Text.Trim();

}

}

void FillProcesses()

{

listView.BeginUpdate();

listView.ItemCheck -= // Ваш код

ManagementObjectSearcher searcher = new ManagementObjectSearcher(

"Select * from Win32_Process");

int n = 0;

foreach (// Ваш код

{

ListViewItem item = new // Ваш код

item.Checked = true;

listView.Items.Add(item);

}

listView.ItemCheck += // Ваш код

listView.EndUpdate();

}

#endregion

#region Events

void listView_ColumnClick(object sender, ColumnClickEventArgs e)

{

bool repeated = listComparer.col == e.Column;

listComparer.col = e.Column;

if (repeated)

{

// Ваш код. Измените порядок сортировки

}

listView.Sort();

}

void listView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)

{

ShowProperty(listView.Items[e.ItemIndex].SubItems[2].Text);

}

void listView_ItemChecked(object sender, ItemCheckedEventArgs e)

{

if (removing)

{

OnUpdateLog("Process " + // Ваш код + " PID = "

+ // Ваш код + " terminated");

e.Item.Remove();

removing = false;

}

}

void listView_ItemCheck(object sender, ItemCheckEventArgs e)

{

ListViewItem item = listView.Items[e.Index];

int id = int.Parse(item.SubItems[2].Text);

string what = // Ваш код + ", PID = " + id;

if (MessageBox.Show("Terminate process?\r\n" + what,

"Terminating process", MessageBoxButtons.YesNoCancel,

MessageBoxIcon.Question) == DialogResult.Yes)

{

StopProcess(id);

removing = true;

}

else

e.NewValue = CheckState.Checked;

}

void btnRefresh_Click(object sender, EventArgs e)

{

listView.Items.Clear();

GetSysInfo();

FillProcesses();

string s = listView.Items[0].SubItems[2].Text;

ShowProperty(s);

}

void rbCurrent_CheckedChanged(object sender, EventArgs e)

{

group.Visible = rbRemote.Checked;

}

void combo_SelectedIndexChanged(object sender, EventArgs e)

{

ManagementClass m = new ManagementClass("Win32_Process");

try

{

m.InvokeMethod("Create", new object[] { combo.Text, 0, 0, 0 });

}

catch (Exception)

{

MessageBox.Show("Process: " + combo.Text + " not found");

}

}

void watcherCreate_EventArrived(object sender, EventArrivedEventArgs e)

{

ManagementBaseObject mo = e.NewEvent["TargetInstance"] as ManagementBaseObject;

ListViewItem item = new ListViewItem(new string[] {

(listView.Items.Count + 1).ToString(),

mo["Caption"].ToString(), mo["ProcessId"].ToString()});

listView.Invoke(new Action<ListViewItem>(AddItem), new object[] { item });

}

void watcherDelete_EventArrived(object sender, EventArrivedEventArgs e)

{

ManagementBaseObject mo = e.NewEvent["TargetInstance"] as ManagementBaseObject;

string name = mo["ProcessId"].ToString();

listView.Invoke(new Action<string>(RemoveList), new object[] { name });

}

void ProcessesControl_Disposed(object sender, EventArgs e)

{

watcherCreate.Stop();

watcherDelete.Stop();

watcherCreate.Dispose();

watcherDelete.Dispose();

}

void OnUpdateLog(string s)

{

if (UpdateLog != null)

UpdateLog(s);

}

#endregion

}

#region ProcessListComparer Class

class ProcessListComparer : IComparer

{

public int col;    // Номер колонки. Количество нажатий

public SortOrder order;   // Режим сортировки

public ProcessListComparer() { col = 0; order = SortOrder.Ascending; }

public int Compare(object x, object y)

{

int res = 0;

string

s1 = ((ListViewItem)x).SubItems[col].Text,

s2 = ((ListViewItem)y).SubItems[col].Text;

switch (col)

{

case 0: res = Int32.Parse(s1).CompareTo(Int32.Parse(s2)); break;

case 1: res = s1.CompareTo(s2); break;

case 2: res = Int32.Parse(s1).CompareTo(Int32.Parse(s2)); break;

}

if (order == SortOrder.Descending)

res = -res;

return res;

}

}

#endregion

}