📄 process.cs
字号:
private void updateStatus(string stringStatus)
{
if (UpdateStatus != null)
UpdateStatus(stringStatus );
}
/// <summary>
/// radioMachine_CheckedChanged event allow user to access services on other machine
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void radioMachine_CheckedChanged(object sender, System.EventArgs e)
{
bool booleanFlag = true;
RadioButton rbClicked = (RadioButton) sender;
if (rbClicked.Text == "&Current Machine")
booleanFlag = false;
//toggle controls
labelIP.Visible = booleanFlag;
textIP.Visible = booleanFlag;
labelUserID.Visible = booleanFlag;
textUserID.Visible = booleanFlag;
labelPassword.Visible = booleanFlag;
textPassword.Visible = booleanFlag;
}
/// <summary>
/// buttonRefresh click event allow user to refresh service list view
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonRefresh_Click(object sender, System.EventArgs e)
{
this.Refresh();
}
/// <summary>
/// List view mouse down event to built context menu dynamically
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listViewProcesses_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
System.Windows.Forms.ListView listViewObject = (System.Windows.Forms.ListView) sender;
ContextMenu mnuContextMenu = new ContextMenu();
MenuItem menuItem;
MenuItem subMenuItem;
//check if right button
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
//set list view item
ProcessItem = listViewObject.GetItemAt(e.X,e.Y);
//create popup menu
listViewProcesses.ContextMenu = mnuContextMenu;
try
{
ProcessID = ProcessItem.SubItems[4].Text;
//right click on ltem add teminate process menu item
menuItem = new MenuItem();
menuItem.Text = "Terminate Process";
mnuContextMenu.MenuItems.Add(menuItem);
// Add functionality to the menu items using the Click event.
menuItem.Click += new System.EventHandler(this.menuItemTerminate_Click);
}
catch (System.NullReferenceException)
{
//no list item selected. Do ignor error
}
//create Create new process menu
menuItem = new MenuItem();
menuItem.Text = "Create New Process";
mnuContextMenu.MenuItems.Add(menuItem);
//Create Calculator submenu
subMenuItem = new MenuItem();
subMenuItem.Text = "Calculator";
menuItem.MenuItems.Add(subMenuItem);
// Add functionality to the menu items using the Click event.
subMenuItem.Click += new System.EventHandler(this.menuItemCalc_Click);
//Create Notepad submenu
subMenuItem = new MenuItem();
subMenuItem.Text = "Notepad";
menuItem.MenuItems.Add(subMenuItem);
// Add functionality to the menu items using the Click event.
subMenuItem.Click += new System.EventHandler(this.menuItemNotepad_Click);
//Create Custom submenu
subMenuItem = new MenuItem();
subMenuItem.Text = "Custom";
menuItem.MenuItems.Add(subMenuItem);
// Add functionality to the menu items using the Click event.
subMenuItem.Click += new System.EventHandler(this.menuItemCustom_Click);
}
}
/// <summary>
/// List view context menu click event to invoke terminate process
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void menuItemTerminate_Click(object sender, System.EventArgs e)
{
ManagementObjectCollection queryCollection;
ListViewItem lvItem;
//Set up a handler for the asynchronous callback
ManagementOperationObserver observer = new ManagementOperationObserver();
completionHandler.MyHandler completionHandlerObj = new completionHandler.MyHandler();
observer.ObjectReady += new ObjectReadyEventHandler(completionHandlerObj.Done);
queryCollection = getProcessCollection("Select * from Win32_Process Where ProcessID = '" + ProcessID + "'");
//Status
updateStatus("Invoking terminate process");
foreach ( ManagementObject mo in queryCollection)
{
//start or stop service
mo.InvokeMethod(observer, "Terminate", null);
}
//wait until invoke method is complete or 5 sec timeout
int intCount = 0;
while (!completionHandlerObj.IsComplete)
{
if (intCount == 10)
{
MessageBox.Show("Terminate process timed out.", "Terminate Process Status");
break;
}
//wait 1/2 sec.
System.Threading.Thread.Sleep(500);
//increment counter
intCount++;
}
if (intCount != 10)
{
//InvokeMethod did not time out
if (completionHandlerObj.ReturnObject.Properties["returnValue"].Value.ToString() == "0")
{
lvItem = ProcessItem;
lvItem.Remove();
}
else
{
MessageBox.Show("Error terminating process.", "Terminate Process");
}
}
//clean-up objects
ProcessID = "";
ProcessItem = null;
//Status
updateStatus("Ready");
this.Update();
}
/// <summary>
/// Create Calculator appication on local or remote machine
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void menuItemCalc_Click(object sender, System.EventArgs e)
{
CreateProcess("Calc.exe");
}
/// <summary>
/// Create Notepad appication on local or remote machine
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void menuItemNotepad_Click(object sender, System.EventArgs e)
{
CreateProcess("Notepad.exe");
}
/// <summary>
/// Create custom appication on local or remote machine
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void menuItemCustom_Click(object sender, System.EventArgs e)
{
string stringApp;
//show dialog box
DialogBox.DialogBoxInput InputForm = new DialogBox.DialogBoxInput();
InputForm.ShowDialog();
if ((string)InputForm.Tag == "OK")
{
stringApp = InputForm.getCreateProcessName;
CreateProcess(stringApp);
}
//cleanup
InputForm.Dispose();
}
/// <summary>
/// Invoke method 'Create' on local or remote machine
/// </summary>
/// <param name="stringCommandLine"></param>
private void CreateProcess(string stringCommandLine)
{
//Set up a handler for the asynchronous callback
ManagementOperationObserver observer = new ManagementOperationObserver();
completionHandler.MyHandler completionHandlerObj = new completionHandler.MyHandler();
observer.ObjectReady += new ObjectReadyEventHandler(completionHandlerObj.Done);
string stringMachineName = "";
//Connect to the remote computer
ConnectionOptions co = new ConnectionOptions();
if (radioMachine.Checked == true)
{
stringMachineName = "localhost";
}
else
{
stringMachineName = textIP.Text;
}
if (stringMachineName.Trim().Length == 0)
{
MessageBox.Show("Must enter machine IP address or name.");
return;
}
//get user and password
if (textUserID.Text.Trim().Length > 0)
{
co.Username = textUserID.Text;
co.Password = textPassword.Text;
}
//Point to machine
System.Management.ManagementScope ms = new System.Management.ManagementScope("\\\\" + stringMachineName + "\\root\\cimv2", co);
//get process path
ManagementPath path = new ManagementPath( "Win32_Process");
//Get the object on which the method will be invoked
ManagementClass processClass = new ManagementClass(ms,path,null);
//Status
updateStatus("Create process " + stringCommandLine + ".");
//Create an array containing all arguments for the method
object[] methodArgs = {stringCommandLine, null, null, 0};
//Execute the method
processClass.InvokeMethod (observer, "Create", methodArgs);
//wait until invoke method is complete or 5 sec timeout
int intCount = 0;
while (!completionHandlerObj.IsComplete)
{
if (intCount > 10)
{
MessageBox.Show("Create process timed out.", "Terminate Process Status");
break;
}
//wait 1/2 sec.
System.Threading.Thread.Sleep(500);
//increment counter
intCount++;
}
if (intCount != 10)
{
//InvokeMethod did not time out
//check for error
if (completionHandlerObj.ReturnObject.Properties["returnValue"].Value.ToString() == "0")
{
//refresh process list
this.Refresh();
}
else
{
MessageBox.Show("Error creating new process.", "Create New Process");
}
}
//Status
updateStatus("Ready");
this.Update();
}
/// <summary>
/// Get computer service collection
/// </summary>
/// <param name="stringQuery"></param>
/// <returns>queryCollection</returns>
private ManagementObjectCollection getProcessCollection(string stringQuery)
{
ManagementObjectSearcher query;
ManagementObjectCollection queryCollection = null;
System.Management.ObjectQuery oq;
string stringMachineName = "";
//Connect to the remote computer
ConnectionOptions co = new ConnectionOptions();
if (radioMachine.Checked == true)
{
stringMachineName = "localhost";
}
else
{
stringMachineName = textIP.Text;
}
if (stringMachineName.Trim().Length == 0)
{
MessageBox.Show("Must enter machine IP address or name.");
return null;
}
//get user and password
if (textUserID.Text.Trim().Length > 0)
{
co.Username = textUserID.Text;
co.Password = textPassword.Text;
}
//Point to machine
System.Management.ManagementScope ms = new System.Management.ManagementScope("\\\\" + stringMachineName + "\\root\\cimv2", co);
//Status
updateStatus("Getting Services Information");
//Query remote computer across the connection
oq = new System.Management.ObjectQuery(stringQuery);
query = new ManagementObjectSearcher(ms,oq);
try
{
queryCollection = query.Get();
}
catch (Exception e1)
{
MessageBox.Show("Error: " + e1);
}
//Status
updateStatus("Ready");
this.Update();
return queryCollection;
}
/// <summary>
/// Get/Set process name
/// </summary>
/// <returns>stringServiceName</returns>
private string ProcessID
{
get
{
return stringProcessID;
}
set
{
stringProcessID = value;
}
}
/// <summary>
/// Get/Set list view item object
/// </summary>
/// <returns>listViewProcessItem</returns>
private ListViewItem ProcessItem
{
get
{
return listViewProcessItem;
}
set
{
listViewProcessItem = value;
}
}
/// <summary>
/// Public Refresh method
/// </summary>
public override void Refresh()
{
// Init list view
InitListView();
getProcess();
boolLoaded = true;
}
/// <summary>
/// Loaded check if control was loaded before
/// </summary>
/// <returns></returns>
public bool Loaded
{
get
{
//true if control already loaded else false.
return boolLoaded;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -