Online CPU Console using a Web control Library and. NET Security (2)

Source: Internet
Author: User
Tags foreach count log tostring trim visual studio
Web Wroxcontrollib
The step one was to the set up a Web control Library. Open Visual Studio. NET and choose a Web Control LibraryProject. 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 ReferencesIn the Solutions Explorer.
Eventlog.cs
Events are accessed through the System.Diagnostics assembly. Use this assembly by making a reference in 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 (O ur assembly), and the message of the event.
Read from the event log by calling EventLog (). Entries to obtain the collection's events from the specified machine. This is 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 are called with a machine and log type, which is set by the client application. The collection returns the earliest event, by default; The log must is turned around to view the newest event. 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 to 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 the ' 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 is viewed and stopped using the System.Diagnostics assembly. After referencing the assembly in the top of the class file, call Process.getprocesses () to retrieve the collection of run Ning processes. Specify which machine these processes are viewed-by setting the machine name parameter.
Processes can be completely stopped by calling the Kill () method. A reference to each thread this a Process is running can also be obtained. Users can also be allowed to stop specific threads as a alternative to stopping the entire process, but it is option To-use and caution as the user may is completely familiar with the internal workings of that application.
The code below requests the collection of processes and loops through with a foreach them. Notice how the collection are called by a machine this 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 the ' 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 this Kill () 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 th E services installed on any given machine.
Services May is 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 are called with a machine this 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 the ' 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>");
}
}
}
The Stop () is called to stop a service, and the Start () 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, we have Web control Library is finished, we are continue by building the Web interface to 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 this. aspx files call a register tag in the top of the document, containing the namespace, assembly name, and tag Prefix of the WROXCPUControl.dll.
vw_eventlog.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.