2010-07-14 5 views

Répondre

5

Si vous essayez d'obtenir les statistiques de l'ordinateur du client, abandonnez; c'est impossible. (Sauf si vous écrivez un plugin de navigateur, que vous vraiment ne devrait pas)

Si vous essayez d'obtenir les statistiques de votre serveur web, c'est très possible.

Voici ma mise en œuvre d'une telle 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 est défini dans le fichier .cs:

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 est une méthode d'extension définie ailleurs:

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"; 
} 
2

cette information est accessible dans le code derrière vous pouvez donc essayer d'utiliser cela combiné avec le pageload pour recueillir les données à utiliser dans la page aspx. Il y a beaucoup plus d'informations accessibles dans le System.Environment alors allez y jeter un coup d'œil!

public override void PageLoad() { 

     string[] logicalDrives = System.Environment.GetLogicalDrives(); 
     //do stuff to put it in the view. 
} 
1

Pour obtenir les informations de machine..you d'un client ne peut pas le faire dans une application Web ... à moins que vous écrivez un contrôle ActiveX ou plug-in Java qui fonctionne sur leur machine.

Mais ce n'est pas conseillé de le faire ... les programmes antivirus et logiciels malveillants peuvent penser que vous ne faites rien.

0

Vous ne pouvez pas dans une page ASP.NET.

Vous pouvez cependant faire un formulaire Windows et publier à l'aide ClickOnce

Lorsque l'utilisateur clique sur un lien sur votre page, vous pouvez apparaitre une forme de fenêtres. Pour afficher ou enregistrer des données, utilisez les services Web. L'utilisateur aura l'impression de ne pas avoir quitté votre site Web.

Vous pouvez utiliser un objet ManagementSearcherObject en C# pour obtenir les informations système, par ex. RAM Dans VB.NET, vous pouvez simplement utiliser My.Computer.

Je n'ai pas travaillé avec les applications de navigateur WPF, mais vous devriez pouvoir faire de même.

Questions connexes