Tutorial :How to get system properties on a webpage?



Question:

I am working on a website in asp.net. I need system properties like RAM, Processor, Hard-drive etc on a web page. How to get it?


Solution:1

If you're trying to get the stats of the client's computer, give up; it's impossible. (Unless you write a browser plugin, which you really shouldn't)

If you're trying to get the stats of your web server, it's very possible.

Here is my implementation of such a page:

<%@ Page Title="Server Stats" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"      CodeFile="Stats.aspx.cs" Inherits="Stats" %>    <%@ Import Namespace="System.Diagnostics" %>  <%@ Import Namespace="Microsoft.VisualBasic.Devices" %>  <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">      <style type="text/css">          body {              background-color: #9DC0E4;          }          table.Details {              width: 550px;              margin-left: -275px;              left: 50%;              position: absolute;          }          table.Details tbody.Group {              border-bottom: solid black 2px;              margin-bottom: 15px;          }          table.Details th.Group {              font-size: x-large;              border-bottom: dashed 1px navy;          }          table.Details th.Name {              text-align: left;          }          table.Details td.Value {              text-align: right;          }      </style>  </asp:Content>  <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">      <%          var computer = new ComputerInfo();          using (var iis = Process.GetCurrentProcess())          using (var cpu = new PerformanceCounter("Processor", "% Processor Time", "_Total"))          using (var uptime = new PerformanceCounter("System", "System Up Time")) {              cpu.NextValue();              uptime.NextValue();      %>      <table class="Details">          <tbody class="Group">              <tr>                  <th class="Group" colspan="2">Environment</th>              </tr>              <tr>                  <th class="Name">Local server time</th>                  <td class="Value">                      <%= DateTime.Now.ToString("F")%></td>              </tr>              <tr>                  <th class="Name">OS</th>                  <td class="Value">                      <%= computer.OSFullName%><br />                      <%= Environment.OSVersion.ToString()%></td>              </tr>              <tr>                  <th class="Name">Machine name</th>                  <td class="Value">                      <%= Environment.MachineName%></td>              </tr>              <tr>                  <th class="Name">User name</th>                  <td class="Value">                      <%= Environment.UserName%></td>              </tr>              <tr>                  <th class="Name">Windows domain</th>                  <td class="Value">                      <%= Environment.UserDomainName%></td>              </tr>          </tbody>          <tbody class="Group">              <tr>                  <th class="Group" colspan="2">IIS</th>              </tr>              <tr>                  <th class="Name">IIS Uptime</th>                  <td class="Value">                      <%= (DateTime.Now- iis.StartTime).ToApproximateString()%></td>              </tr>              <tr>                  <th class="Name">Priority</th>                  <td class="Value">                      <%= iis.PriorityClass%></td>              </tr>              <tr>                  <th class="Name">Physical Memory Used</th>                  <td class="Value">                      <%= ToSizeString(iis.WorkingSet64)%></td>              </tr>              <tr>                  <th class="Name">Virtual Memory Used</th>                  <td class="Value">                      <%= ToSizeString(iis.VirtualMemorySize64)%></td>              </tr>          </tbody>          <tbody class="Group">              <tr>                  <th class="Group" colspan="2">Hardware</th>              </tr>              <tr>                  <th class="Name">Processors</th>                  <td class="Value">                      <%= Environment.ProcessorCount.ToString()%></td>              </tr>              <tr>                  <th class="Name">Physical memory</th>                  <td class="Value">                      <%= ToSizeString(computer.TotalPhysicalMemory)%></td>              </tr>              <tr>                  <th class="Name">Virtual memory</th>                  <td class="Value">                      <%= ToSizeString(computer.TotalVirtualMemory)%></td>              </tr>          </tbody>          <tbody class="Group">              <tr>                  <th class="Group" colspan="2">Performance</th>              </tr>              <tr>                  <th class="Name">Uptime</th>                  <td class="Value">                      <%= TimeSpan.FromSeconds(uptime.NextValue()).ToApproximateString()%>                  </td>              </tr>              <tr>                  <th class="Name">CPU Usage</th>                  <td class="Value">                      <%= (cpu.NextValue()/100).ToString("p")%>                  </td>              </tr>              <tr>                  <th class="Name">Physical memory free</th>                  <td class="Value">                      <%= ToSizeString(computer.AvailablePhysicalMemory)%></td>              </tr>              <tr>                  <th class="Name">Virtual memory free</th>                  <td class="Value">                      <%= ToSizeString(computer.AvailableVirtualMemory)%></td>              </tr>          </tbody>      </table>      <%} %>  </asp:Content>  

ToSizeString is defined in the .cs file:

protected static string ToSizeString(double bytes) {      var culture = CultureInfo.CurrentUICulture;      const string format = "#,0.0";        if (bytes < 1024)          return bytes.ToString("#,0", culture);      bytes /= 1024;      if (bytes < 1024)          return bytes.ToString(format, culture) + " KB";      bytes /= 1024;      if (bytes < 1024)          return bytes.ToString(format, culture) + " MB";      bytes /= 1024;      if (bytes < 1024)          return bytes.ToString(format, culture) + " GB";      bytes /= 1024;      return bytes.ToString(format, culture) + " TB";  }  

ToApproximateString is an extension method defined elsewhere:

public static string ToApproximateString(this TimeSpan time) {      if (time.TotalDays > 14)          return ((int)(time.TotalDays / 7)).ToString("#,0.0") + " weeks";      if (14 - time.TotalDays < .75)          return "two weeks";      if (time.TotalDays > 1)          return time.TotalDays.ToString("#,0.0") + " days";      else if (time.TotalHours > 1)          return time.TotalHours.ToString("#,0.0") + " hours";      else if (time.TotalMinutes > 1)          return time.TotalMinutes.ToString("#,0.0") + " minutes";      else          return time.TotalSeconds.ToString("#,0.0") + " seconds";  }  


Solution:2

this info is accessible in the code behind so you can try to use that combined with the pageload to gather the data for use in the aspx page. There is a lot more info accessible in the System.Environment so go check it out!

public override void PageLoad() {             string[] logicalDrives  = System.Environment.GetLogicalDrives();           //do stuff to put it in the view.  }  


Solution:3

To get the info of a client's machine..you can't do that in a Web App...unless you write an ActiveX control or Java plugin that runs on their machine.

But's not advisable that you do that...antivirus and malware programs may think you're up to no good.


Solution:4

You can't in an ASP.NET page.

You can, however, make a Windows Form and publish it using ClickOnce

When the user clicks on a link on your page, you can popup a windows form. To display or save data, use web services. The user will feel like he hasn't left your web site.

You can use a ManagementSearcherObject in C# to get the system information e.g. RAM In VB.NET you can just use My.Computer.

I haven't worked with WPF browser applications, but you should be able to do the same there.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »