Online CPU Console using a Web Control Library wit
2024-07-21 02:16:43
供稿:网友
wroxcontrollib
step one is to set up a web control library. open visual studio .net and choose a web control library project. add three new custom web controls to the project named evenlog.cs, process.cs, and services.cs. add system.serviceprocess as a reference to the project by right-clicking references in the solutions explorer.
eventlog.cs
events are accessed through the system.diagnostics assembly. use this assembly by making a reference at the top of your class.
write to the event log by calling eventlog.writeentry(). the writeentry() method provides many parameters such as the type of event (info, error, etc.), the source of the event (our assembly), and the message of the event.
read from the event log by calling eventlog().entries to obtain the collection of events from the specified machine. this method may be called with parameters such as log type (application, system, security), and machine name.
the code below requests the collection of events and loops through them with a for statement. notice how the collection is called with a machine and log type, that is set by the client application. the collection returns the earliest event first by default; the log must be turned around to view the newest event first. reverse the order by iterating through the collection backwards. print the content out to the client application using the htmltextwriter. htmltextwriter is the default parameter passed into a custom control. let's take a look at the code for eventlog.cs:
using system;
using system.web.ui;
using system.web.ui.webcontrols;
using system.componentmodel;
using system.diagnostics; //add this reference
public class eventlog : system.web.ui.webcontrols.webcontrol
{
private string logtype = "application";
private string machine = "";
//machine is a property that is set by the client
public string machine
{
get
{
return machine;
}
set
{
machine = value;
}
}
protected override void render(htmltextwriter doc)
{
//call the collection of events
eventlogentrycollection objeventcol = new eventlog(logtype,machine).entries;
//loop through the collection of events
for(int icount<objeventcol.count;icount=objeventcol.count; icount>=0; icount++)
{
//write to the document
doc.write(objeventcol.entrytype.tostring() + " - ");
doc.write(objeventcol[i].source + "<br>");
doc.write(objeventcol[i].message + "<p>");
}
}
}
process.cs
processes may be viewed and stopped using the system.diagnostics assembly. after referencing the assembly at the top of the class file, call process.getprocesses() to retrieve the collection of running processes. specify which machine these processes are viewed from by setting the machine name parameter.
processes can be completely stopped by calling the kill() method. a reference to each thread that a process is running can also be obtained. users can also be allowed to stop specific threads as an alternative to stopping the entire process, but this is an option to use with caution as the user may not be completely familiar with all the internal workings of that application.
the code below requests the collection of processes and loops through them with a foreach statement. notice how the collection is called by a machine that is set by the client application. the contents are then printed out to the client application using the htmltextwriter. let's take a look at the code for process.cs:
using system;
using system.web.ui;
using system.web.ui.webcontrols;
using system.componentmodel;
using system.diagnostics; //add this reference
public class process : system.web.ui.webcontrols.webcontrol
{
private string machine = "";
private string stopprocess = "";
//machine is a property that is set by the client
public string machine
{
get
{
return machine;
}
set
{
machine = value;
}
}
protected override void render(htmltextwriter doc)
{
//loop through the collection of processes
foreach(process objp in process.getprocesses(machine))
{
//write to the document
doc.write(objp.processname + " - ");
doc.write(objp.threads.count.tostring() + " - ");
doc.write(objp.totalprocessortime.tostring() + "<br>");
}
}
}
notice in the code that the kill() method is called to completely stop a process while looping through the collection.
foreach(process objp in process.getprocesses(machine))
{
if(objp.processname.trim() == stopprocess.trim())
{
//stops the process
objp.kill();
}
}
service.cs
services can be viewed, stopped, started, paused, and resumed using the system.serviceprocess assembly. reference the assembly at the top of the class file and call servicecontroller.getservices() to receive a collection of the services installed on any given machine.
services may be started by calling the start() method, and stopped by calling the stop() method.
in the code below, we request the collection of services, and loop through them with a foreach statement. notice how the collection is called with a machine that is set by the client application. we print the content out to the client application using the htmltextwriter. let's take a look at the code for service.cs:
using system;
using system.web.ui;
using system.web.ui.webcontrols;
using system.componentmodel;
using system.serviceprocess; //add this reference
public class services : system.web.ui.webcontrols.webcontrol
{
private string machine = "";
private string stopservice = "";
private string startservice = "";
//machine is a property that is set by the client
public string machine
{
get
{
return machine;
}
set
{
machine = value;
}
}
protected override void render(htmltextwriter doc)
{
//loop through the collection of services
foreach(servicecontroller objsp in servicecontroller.getservices(machine))
{
//write to the document
doc.write(objsp.displayname + " - ");
doc.write(objsp.status.tostring() + " - ");
doc.write(objsp.servicetype.tostring() + "<br>");
}
}
}
note that the stop() method is called to stop a service, and the start() method is called to start a service while looping through the collection.
foreach(servicecontroller objsp in servicecontroller.getservices(machine))
{
if(objsp.displayname.trim() == stopservice.trim())
{
//stop a service
objsp.stop();
}
if(objsp.displayname.trim() == startservice.trim())
{
/start a service
objsp.start();
}
}
wroxcpuconsole
now that our web control library is finished, we continue by building the web interface for this application. open visual studio and choose an [i]asp.net web application project. add the following three forms: vw_eventlog.aspx, vw_process.aspx, and vw_service.aspx.
note that the .aspx files call a register tag at the top of the document, containing the namespace, assembly name, and tag prefix of the wroxcpucontrol.dll.
vw_eventlog.aspx注册会员,创建你的web开发资料库,