社区
C#
帖子详情
求c#获取系统资源的代码和思路
wangxq_98
2004-10-27 10:56:03
包括cpu,内存,网络资源的使用
...全文
175
13
打赏
收藏
求c#获取系统资源的代码和思路
包括cpu,内存,网络资源的使用
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
13 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
haonanernet
2004-10-30
打赏
举报
回复
//
// groupBox1
//
this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button2,
this.button3,
this.button1});
this.groupBox1.Location = new System.Drawing.Point(16, 272);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(328, 64);
this.groupBox1.TabIndex = 4;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "进程管理";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(376, 349);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.groupBox1,
this.panel1});
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "系统进程管理";
this.panel1.ResumeLayout(false);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
//构造打开文件对话框
OpenFileDialog ofDlg = new OpenFileDialog();
//选择可执行文件
ofDlg.Filter = "可执行文件 (*.exe)|*.exe|所有文件(*.*)|*.*";
//显示对话框
if(ofDlg.ShowDialog() == DialogResult.OK)
{
try
{
//启动选择的程序
Process newProcess = Process.Start(ofDlg.FileName);
newProcess.EnableRaisingEvents=true;
//为它添加退出进程的事件代理
newProcess.Exited += new EventHandler(OnProcessExited);
//更新列表
UpdateProcess();
}
//参数错误异常
catch(ArgumentException aError)
{
MessageBox.Show("出现错误:"+aError.Message, "警告",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
}
private void OnProcessExited(object sender, EventArgs e)
{
//刷新ListView控件
UpdateProcess();
}
private void button3_Click(object sender, System.EventArgs e)
{
//刷新ListView控件
UpdateProcess();
}
private void button2_Click(object sender, System.EventArgs e)
{
if(this.listView1.SelectedItems.Count>0)
{
//获取选中项目中的ID值
int processID = Int32.Parse(listView1.SelectedItems[0].SubItems[1].Text);
//根据ID获取进程
Process selectedProcess = Process.GetProcessById(processID);
selectedProcess.EnableRaisingEvents=true;
//为它添加退出进程的事件代理
selectedProcess.Exited += new EventHandler(OnProcessExited);
//如果进程还没有退出
if(!selectedProcess.HasExited)
{
//如果关闭主窗口失败
if(!selectedProcess.CloseMainWindow())
{
//提示是否要杀死进程
if(MessageBox.Show("不能关闭应用程序窗口,需要继续终止进程吗?",
"提示",MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
try
{
//杀死进程
selectedProcess.Kill();
//更新列表
UpdateProcess();
}
catch(Exception excep)
{
MessageBox.Show(excep.Message, "不能结束进程", MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
}
}
else
{
MessageBox.Show("选中的进程已经退出!", "提示",MessageBoxButtons.OK, MessageBoxIcon.Information);
UpdateProcess();
}
}
else
{
MessageBox.Show("请先选中一个当前正在运行的进程!", "提示", MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
}
}
haonanernet
2004-10-30
打赏
举报
回复
//判断优先级的值
if (priorityNumber==13){prior="高";};
if (priorityNumber==4){prior="低";};
if (priorityNumber==8){prior="标准";};
if (priorityNumber==24){prior="实时";};
//进程信息
string[] subItems={
processName,
processID.ToString(),
threadNumber.ToString(),
String.Format("{0:00}:{1:00}:{2:00}",
cpuTime.Hours, cpuTime.Minutes, cpuTime.Seconds),
String.Format("{0:#,#,#}K", memory / 1024),
prior,
startTime.ToLongTimeString()
};
//构造一项并插入
ListViewItem item=new ListViewItem(subItems);
this.listView1.Items.Insert(i,item);
}
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.panel1 = new System.Windows.Forms.Panel();
this.listView1 = new System.Windows.Forms.ListView();
this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
this.columnHeader5 = new System.Windows.Forms.ColumnHeader();
this.columnHeader6 = new System.Windows.Forms.ColumnHeader();
this.columnHeader7 = new System.Windows.Forms.ColumnHeader();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.panel1.SuspendLayout();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.listView1});
this.panel1.Location = new System.Drawing.Point(16, 16);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(328, 240);
this.panel1.TabIndex = 0;
//
// listView1
//
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1,
this.columnHeader2,
this.columnHeader3,
this.columnHeader4,
this.columnHeader5,
this.columnHeader6,
this.columnHeader7});
this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.listView1.FullRowSelect = true;
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(328, 240);
this.listView1.TabIndex = 0;
this.listView1.View = System.Windows.Forms.View.Details;
//
// columnHeader1
//
this.columnHeader1.Text = "进程名";
//
// columnHeader2
//
this.columnHeader2.Text = "PID";
this.columnHeader2.Width = 42;
//
// columnHeader3
//
this.columnHeader3.Text = "线程数";
//
// columnHeader4
//
this.columnHeader4.Text = "cpu占用时间";
//
// columnHeader5
//
this.columnHeader5.Text = "占用内存";
//
// columnHeader6
//
this.columnHeader6.Text = "优先级";
//
// columnHeader7
//
this.columnHeader7.Text = "启动时间";
//
// button1
//
this.button1.Location = new System.Drawing.Point(32, 24);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "新建进程";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(128, 24);
this.button2.Name = "button2";
this.button2.TabIndex = 2;
this.button2.Text = "结束进程";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(224, 24);
this.button3.Name = "button3";
this.button3.TabIndex = 3;
this.button3.Text = "刷新列表";
this.button3.Click += new System.EventHandler(this.button3_Click);
haonanernet
2004-10-30
打赏
举报
回复
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;
namespace ch4_1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.Windows.Forms.ColumnHeader columnHeader3;
private System.Windows.Forms.ColumnHeader columnHeader4;
private System.Windows.Forms.ColumnHeader columnHeader5;
private System.Windows.Forms.ColumnHeader columnHeader6;
private System.Windows.Forms.ColumnHeader columnHeader7;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
UpdateProcess();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void UpdateProcess()
{
//代表进程名
string processName;
//代表进程号
int processID;
//线程数
int threadNumber;
//cpu时间
TimeSpan cpuTime;
//占用的物理内存
int memory;
//优先级
int priorityNumber;
//启动时间
DateTime startTime;
//获取当前系统中的所有进程
Process[] processes= Process.GetProcesses();
//清除列表视图中原有的内容
if (this.listView1!=null)
{
this.listView1.Items.Clear();
}
//在列表中加入每个进程项目的详细信息
for(int i = 0; i < processes.Length; i++)
{
processName=processes[i].ProcessName;
processID=processes[i].Id;
threadNumber=processes[i].Threads.Count;
cpuTime=processes[i].TotalProcessorTime;
memory=processes[i].WorkingSet;
priorityNumber=processes[i].BasePriority;
startTime=processes[i].StartTime;
string prior="";
wangxq_98
2004-10-29
打赏
举报
回复
这种方法获取CPU利用率
System.Diagnostics .PerformanceCounter CpuCounter = new PerformanceCounter ();
CpuCounter.CategoryName ="Processor";
CpuCounter.CounterName ="% Processor Time";
CpuCounter.InstanceName ="_Total";
string CpuValue=CpuCounter.NextValue ().ToString ();
为什么CpuValue老为0
生活真美好
2004-10-29
打赏
举报
回复
gz
wangxq_98
2004-10-27
打赏
举报
回复
难道真没有人知道吗?
wangxq_98
2004-10-27
打赏
举报
回复
谢谢 CMIC
可能是我没有说清楚。
我想要的是一个类似于windows任务管理器的东西。
zhouzh197895
2004-10-27
打赏
举报
回复
up
CMIC
2004-10-27
打赏
举报
回复
参考:http://dev.csdn.net/develop/article/27/27654.shtm
wangxq_98
2004-10-27
打赏
举报
回复
在线等待中
daou101
2004-10-27
打赏
举报
回复
使用WMI,参考:
http://www.codeproject.com/
的WMI栏目。
wangxq_98
2004-10-27
打赏
举报
回复
自己再顶一下。看看其他的高手有什么意见。
jkflyfox
2004-10-27
打赏
举报
回复
可以用System.Diagnostics.Process.GetProcesses()取得当前机器上运行的所有进程,并取得其占用的内存,但是网络什么的就不知道了。
你看下孟子E章的:
http://xml.sz.luohuedu.net/xml/Content.asp
C#
代码
审计实战+前置知识
C#
基于 C 和 C++ 编程语言,是一个简单的、现代的、通用的、面向对象的编程语言,它是由微软(Microsoft)开发的,由 Ecma 和 ISO 核准认可的。
C#
是由 Anders Hejlsberg 和他的团队在 .Net 框架开发期间开发的。
C#
...
[
代码
审计篇 ]
代码
审计
思路
详解
渗透测试岗几乎都会问道
代码
审计,这篇文章就来聊一聊
代码
审计的
思路
。 这篇文章就来聊一聊什么是
代码
审计,什么是静态测试 ? 重点部分:
代码
审计工具介绍,
代码
审计的
思路
介绍。 不要觉得
代码
审计很容易,也不要...
代码
审计
思路
详解
代码
审计(Code audit)是一种以发现程序错误,安全漏洞和违反程序规范为目标的源
代码
分析。软件
代码
审计是对编程项目中源
代码
的全面分析,旨在发现错误,安全漏洞或违反编程约定。 它是防御性编程范例的一个组成...
C#
基础之
C#
代码
的注意事项
C#
基础之
C#
代码
的注意事项 关于
代码
优化的问题,之前也给大家介绍过相关的内容。下面介绍的是
C#
代码
优化的一些注意事项,供参考。 一、用属性代替可访问的字段 1、.NET数据绑定只支持数据绑定,使用...
C#
语言实现《学生成绩管理系统》
这次数据库自编项目,使我掌握了
C#
的编写技巧以及SQLServer数据库和VS的链接方式,其中遇到过不少的难题,但是只要抓住
思路
检查之后还是能做到及时纠正,对每个部分了解是极为重要的一点。我创建了一个VS项目连接...
C#
110,502
社区成员
642,567
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章