C#2.0 获取服务器CPU及内存使用情况
最近服务器比较慢,但不知道什么原因。想写个代码来看看使用情况,希望大家不希赐教最好有代码或例子哦先谢过了...
最近服务器比较慢,但不知道什么原因。
想写个代码来看看使用情况,希望大家不希赐教
最好有代码或例子哦
先谢过了 展开
想写个代码来看看使用情况,希望大家不希赐教
最好有代码或例子哦
先谢过了 展开
4个回答
展开全部
//cpu频率
using Microsoft.Win32;
private int GetCPUFrequency()
{
RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"HARDWARE\DESCRIPTION\System\CentralProcessor\0");
object obj = rk.GetValue("~MHz");
int CPUFrequency = (int)obj;
return CPUFrequency;
}
//////////////////////////////////
//磁盘空间 Management
using System.Management;
private long GetFreeDiskSpace()
{
ManagementObject disk = new ManagementObject(
"win32_logicaldisk.deviceid=\"d:\"");
disk.Get();
string totalByte = disk["FreeSpace"].ToString();
long freeDiskSpaceMb = Convert.ToInt64(totalbyte)/1024/1024;
return freeDiskSpaceMb;
}
/////////////////////
//内存信息
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
/**//// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
[StructLayout(LayoutKind.Sequential)]
public struct MEMORY_INFO
{
public uint dwLength;
public uint dwMemoryLoad;
public uint dwTotalPhys;
public uint dwAvailPhys;
public uint dwTotalPageFile;
public uint dwAvailPageFile;
public uint dwTotalVirtual;
public uint dwAvailVirtual;
}
[DllImport("kernel32")]
public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);
public static int Main(string[] args)
{
Class1 class1 = new Class1();
class1.GetMemoryStatus();
return 0;
}
private void GetMemoryStatus()
{
MEMORY_INFO MemInfo;
MemInfo = new MEMORY_INFO();
GlobalMemoryStatus(ref MemInfo);
long totalMb = Convert.ToInt64( MemInfo.dwTotalPhys.ToString())/1024/1024;
long avaliableMb = Convert.ToInt64( MemInfo.dwAvailPhys.ToString())/1024/1024;
Console.WriteLine( "物理内存共有" + totalMb + " MB");
Console.WriteLine( "可使用的物理内存有" + avaliableMb +" MB");
}
}
//////////////////////////////
//cpu名字
using Microsoft.Win32;
private string GetCPUName()
{
RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"HARDWARE\DESCRIPTION\System\CentralProcessor\0");
object obj = rk.GetValue("ProcessorNameString");
string CPUName = (string)obj;
return CPUName.TrimStart();
}
///////////////////////
//OS版本
using System;
namespace determineOS_CS
{
class Class1
{
static void Main(string[] args)
{
// Get OperatingSystem information from the system namespace.
System.OperatingSystem osInfo =System.Environment.OSVersion;
// Determine the platform.
switch(osInfo.Platform)
{
// Platform is Windows 95, Windows 98,
// Windows 98 Second Edition, or Windows Me.
case System.PlatformID.Win32Windows:
switch (osInfo.Version.Minor)
{
case 0:
Console.WriteLine ("Windows 95");
break;
case 10:
if(osInfo.Version.Revision.ToString()=="2222A")
Console.WriteLine("Windows 98 Second Edition");
else
Console.WriteLine("Windows 98");
break;
case 90:
Console.WriteLine("Windows Me");
break;
}
break;
// Platform is Windows NT 3.51, Windows NT 4.0, Windows 2000,
// or Windows XP.
case System.PlatformID.Win32NT:
switch(osInfo.Version.Major)
{
case 3:
Console.WriteLine("Windows NT 3.51");
break;
case 4:
Console.WriteLine("Windows NT 4.0");
break;
case 5:
if (osInfo.Version.Minor==0)
Console.WriteLine("Windows 2000");
else
Console.WriteLine("Windows XP");
break;
}break;
}
Console.ReadLine ();
}
}
}
using Microsoft.Win32;
private int GetCPUFrequency()
{
RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"HARDWARE\DESCRIPTION\System\CentralProcessor\0");
object obj = rk.GetValue("~MHz");
int CPUFrequency = (int)obj;
return CPUFrequency;
}
//////////////////////////////////
//磁盘空间 Management
using System.Management;
private long GetFreeDiskSpace()
{
ManagementObject disk = new ManagementObject(
"win32_logicaldisk.deviceid=\"d:\"");
disk.Get();
string totalByte = disk["FreeSpace"].ToString();
long freeDiskSpaceMb = Convert.ToInt64(totalbyte)/1024/1024;
return freeDiskSpaceMb;
}
/////////////////////
//内存信息
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
/**//// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
[StructLayout(LayoutKind.Sequential)]
public struct MEMORY_INFO
{
public uint dwLength;
public uint dwMemoryLoad;
public uint dwTotalPhys;
public uint dwAvailPhys;
public uint dwTotalPageFile;
public uint dwAvailPageFile;
public uint dwTotalVirtual;
public uint dwAvailVirtual;
}
[DllImport("kernel32")]
public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);
public static int Main(string[] args)
{
Class1 class1 = new Class1();
class1.GetMemoryStatus();
return 0;
}
private void GetMemoryStatus()
{
MEMORY_INFO MemInfo;
MemInfo = new MEMORY_INFO();
GlobalMemoryStatus(ref MemInfo);
long totalMb = Convert.ToInt64( MemInfo.dwTotalPhys.ToString())/1024/1024;
long avaliableMb = Convert.ToInt64( MemInfo.dwAvailPhys.ToString())/1024/1024;
Console.WriteLine( "物理内存共有" + totalMb + " MB");
Console.WriteLine( "可使用的物理内存有" + avaliableMb +" MB");
}
}
//////////////////////////////
//cpu名字
using Microsoft.Win32;
private string GetCPUName()
{
RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"HARDWARE\DESCRIPTION\System\CentralProcessor\0");
object obj = rk.GetValue("ProcessorNameString");
string CPUName = (string)obj;
return CPUName.TrimStart();
}
///////////////////////
//OS版本
using System;
namespace determineOS_CS
{
class Class1
{
static void Main(string[] args)
{
// Get OperatingSystem information from the system namespace.
System.OperatingSystem osInfo =System.Environment.OSVersion;
// Determine the platform.
switch(osInfo.Platform)
{
// Platform is Windows 95, Windows 98,
// Windows 98 Second Edition, or Windows Me.
case System.PlatformID.Win32Windows:
switch (osInfo.Version.Minor)
{
case 0:
Console.WriteLine ("Windows 95");
break;
case 10:
if(osInfo.Version.Revision.ToString()=="2222A")
Console.WriteLine("Windows 98 Second Edition");
else
Console.WriteLine("Windows 98");
break;
case 90:
Console.WriteLine("Windows Me");
break;
}
break;
// Platform is Windows NT 3.51, Windows NT 4.0, Windows 2000,
// or Windows XP.
case System.PlatformID.Win32NT:
switch(osInfo.Version.Major)
{
case 3:
Console.WriteLine("Windows NT 3.51");
break;
case 4:
Console.WriteLine("Windows NT 4.0");
break;
case 5:
if (osInfo.Version.Minor==0)
Console.WriteLine("Windows 2000");
else
Console.WriteLine("Windows XP");
break;
}break;
}
Console.ReadLine ();
}
}
}
展开全部
ServerOS = Environment.OSVersion.ToString(); //操作系统:
CpuSum = Environment.GetEnvironmentVariable("NUMBER_OF_PROCESSORS"); //CPU个数:
CpuType = Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER"); //CPU类型:
ServerSoft = Request.ServerVariables["SERVER_SOFTWARE"]; //信息服务软件:
MachineName = Server.MachineName; //服务器名
ServerName = Request.ServerVariables["SERVER_NAME"]; //服务器域名
ServerPath = Request.ServerVariables["APPL_PHYSICAL_PATH"]; //虚拟服务绝对路径
ServerNet = ".NET CLR " + Environment.Version.ToString(); //DotNET 版本
ServerArea = (DateTime.Now - DateTime.UtcNow).TotalHours > 0 ? "+" + (DateTime.Now - DateTime.UtcNow).TotalHours.ToString() : (DateTime.Now - DateTime.UtcNow).TotalHours.ToString(); //服务器时区
ServerTimeOut = Server.ScriptTimeout.ToString(); //脚本超时时间
ServerStart = ((Double)System.Environment.TickCount / 3600000).ToString("N2"); //开机运行时长
PrStart = GetPrStart(); //进程开始时间
AspNetN = GetAspNetN(); //AspNet 内存占用
AspNetCpu = GetAspNetCpu(); //AspNet CPU时间
ServerSessions = Session.Contents.Count.ToString(); //Session总数
ServerApp = Application.Contents.Count.ToString(); //Application总数
ServerCache = Cache.Count.ToString(); //应用程序缓存总数
ServerAppN = GetServerAppN(); //应用程序占用内存
ServerFso = Check("Scripting.FileSystemObject"); //FSO 文本文件读写
ServerTimeOut = Server.ScriptTimeout.ToString() + "毫秒"; //本页执行时间
CpuSum = Environment.GetEnvironmentVariable("NUMBER_OF_PROCESSORS"); //CPU个数:
CpuType = Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER"); //CPU类型:
ServerSoft = Request.ServerVariables["SERVER_SOFTWARE"]; //信息服务软件:
MachineName = Server.MachineName; //服务器名
ServerName = Request.ServerVariables["SERVER_NAME"]; //服务器域名
ServerPath = Request.ServerVariables["APPL_PHYSICAL_PATH"]; //虚拟服务绝对路径
ServerNet = ".NET CLR " + Environment.Version.ToString(); //DotNET 版本
ServerArea = (DateTime.Now - DateTime.UtcNow).TotalHours > 0 ? "+" + (DateTime.Now - DateTime.UtcNow).TotalHours.ToString() : (DateTime.Now - DateTime.UtcNow).TotalHours.ToString(); //服务器时区
ServerTimeOut = Server.ScriptTimeout.ToString(); //脚本超时时间
ServerStart = ((Double)System.Environment.TickCount / 3600000).ToString("N2"); //开机运行时长
PrStart = GetPrStart(); //进程开始时间
AspNetN = GetAspNetN(); //AspNet 内存占用
AspNetCpu = GetAspNetCpu(); //AspNet CPU时间
ServerSessions = Session.Contents.Count.ToString(); //Session总数
ServerApp = Application.Contents.Count.ToString(); //Application总数
ServerCache = Cache.Count.ToString(); //应用程序缓存总数
ServerAppN = GetServerAppN(); //应用程序占用内存
ServerFso = Check("Scripting.FileSystemObject"); //FSO 文本文件读写
ServerTimeOut = Server.ScriptTimeout.ToString() + "毫秒"; //本页执行时间
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
windows优化大师
超级兔子
深度系统优化工具
都可以
超级兔子
深度系统优化工具
都可以
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
WMI,具体写法网上很多。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询