Online CPU Console using a Web Control Library with .NET Security(2)

web

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

时间: 2024-08-22 14:39:53

Online CPU Console using a Web Control Library with .NET Security(2)的相关文章

Online CPU Console using a Web Control Library with .NET Security(1)

web ABSTRACTAdministering applications and servers when not connected to the network can be a nightmare, especially when only a few people manage the application. Just imagine going out for an evening on the town and then you're paged at one o'clock

Online CPU Console using a Web Control Library with .NET Security(3)

web Call the event viewer control by the tag prefix colon and the class name of eventlog.cs. Note that we set a property from the query string in the code behind file.Display vw_EventLog.aspx file < !-- Register the WROXControlLib Assembly  -- ><

Online CPU Console using a Web Control Library wit

WROXControlLibStep 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

c#-Windows Forms Control Library of C#

问题描述 Windows Forms Control Library of C# VS 2013里面没有C#的Windows Forms Control Library 解决方案 我刚在我的VS2013里试过了,是可以找到 Windows Forms Control Library 这个模板的 解决方案二: wpf里面有 如果没有找到可以去网上下载 解决方案三: 有的,不过你要新建windows forms程序.另外,你需要安装完整版的vs,而不是visual studio express fo

wpf-WPF 看不到WPF control library 和 WPF custom class library 工程

问题描述 WPF 看不到WPF control library 和 WPF custom class library 工程 我的VS2010 怎么不能创建WPF control library 和 WPF custom class library

【更正】“给自定义控件(Web Control)添加事件的几种方法”有一个不太准确的地方。

    给自定义控件(Web Control)添加事件的几种方法.前两种方法可以不实现IPostBackEventHandler           上一篇写了一下如何在自定义控件里面添加事件,由简单的开始,一步一步实现了几种添加事件的方式,由于当时只给自定义控件添加了一种外部事件,测试的时候没有什么问题,但是后来在写分页控件的时候,我给分页控件加了两种外部事件,然后测试的时候就出现了一个问题,本来只想调用外部的一种事件,结果外部的两种事件都被调用了.分析了一下,public event Eve

A Complete ActiveX Web Control Tutorial

Download demo project - 231 Kb Introduction ActiveX is a Microsoft technology developed in the mid 90's, that allows for the creation of applet-like applications that can be downloaded and run within Microsoft's Web browser. This article is intended

把Web Control导出为Excel或Word

excel|web|word /// <summary> /// 将Web控件导出 /// </summary> /// <param name="source">控件实例</param> /// <param name="type">类型:Excel或Word</param> public void ExpertControl(System.Web.UI.Control source, Doc

在C#代码里使用IE WEB Control TreeView

treeview|web 网站目录下需要有Microsoft.Web.UI.WebControls.dll和相应的文件 如大家要转载,请保留本人的版权. /* *Description:完全的操作XML文件 *Auther:mingziweb_天很蓝 *Email:chongchong2008@msn.com *Dates:22004-09-10 *Copyright:ChongChong2008 YiChang HuBei China */ .aspx <%@ Register TagPref