您的位置:首页 > 编程语言 > C#

C#实现对远程服务器的内存和CPU监控

2016-07-04 20:50 393 查看
C#实现对远程服务器的内存和CPU监控小记

1、 主要使用到的组件有System.Management.dll

2、 主要类为 :ManagementScope

连接远程服务器示例代码:

public delegate void DelReadStdOutput(string result);
public delegate void DelReadErrOutput(string result);
public event DelReadStdOutput ReadStdOutput;
public event DelReadErrOutput ReadErrOutput;
/// <summary>
/// 注册事件
/// </summary>
private void Init()
{
ReadStdOutput += new DelReadStdOutput(ReadStdOutputAction);
ReadErrOutput += new DelReadErrOutput(ReadErrOutputAction);

}
//----------------------部分实现代码==================
using (p = new Process())
{

//pause --  停止
p.StartInfo.FileName = fileName;

p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;

p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.EnableRaisingEvents = true;
p.Exited += new EventHandler(p_Exited);
p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);

p.Start();
//读取输出:
p.BeginErrorReadLine();
p.BeginOutputReadLine();
p.WaitForExit();
}

void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
this.lab_tips.Text = "开始读取数据...读取时间可能有点长,请耐心等待!";
if (e.Data != null)
{
this.Invoke(ReadStdOutput, new object[] { e.Data });
}
}
// 读取进程信息:
public void ReadStdOutputAction(string s)
{

if (num > 1)
{
budiler.AppendLine(s);
}
this.textBox1.Text = budiler.ToString();
num++;
}


View Code
暂时就到这儿了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: