目前可用于realtek的网卡,别的部份网卡也能用,有时间再完善,这里只是贴一个编写这类软件的思路。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Management;
using System.Runtime.InteropServices;
using System.IO;
using System.Net.Sockets;
using Ziri;
namespace ReadStream
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Timer timer1;
private System.ComponentModel.IContainer components;
private System.Windows.Forms.ListBox listBox1;
private System.Diagnostics.PerformanceCounter per;
private string systemname="";
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtIP;
private System.Windows.Forms.TextBox txtUserName;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtPassWord;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button btSure;
private System.Windows.Forms.TextBox txtMac;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button btCancel;
private string macname="";
private float[] dynamicx;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button btFanTi;
private System.Windows.Forms.Button btEnglish;
private System.Windows.Forms.Button btJianTi;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.TextBox txtMachineName;
private System.Windows.Forms.Label label8;
private float[] dynamicy;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.textBox2 = new System.Windows.Forms.TextBox();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.listBox1 = new System.Windows.Forms.ListBox();
this.label1 = new System.Windows.Forms.Label();
this.txtIP = new System.Windows.Forms.TextBox();
this.txtUserName = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.txtPassWord = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.btSure = new System.Windows.Forms.Button();
this.txtMac = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.btCancel = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.btFanTi = new System.Windows.Forms.Button();
this.btEnglish = new System.Windows.Forms.Button();
this.btJianTi = new System.Windows.Forms.Button();
this.label7 = new System.Windows.Forms.Label();
this.txtMachineName = new System.Windows.Forms.TextBox();
this.label8 = new System.Windows.Forms.Label();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// textBox2
//
this.textBox2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.textBox2.Location = new System.Drawing.Point(88, 544);
this.textBox2.Name = "textBox2";
this.textBox2.ReadOnly = true;
this.textBox2.Size = new System.Drawing.Size(186, 21);
this.textBox2.TabIndex = 0;
this.textBox2.Text = "";
//
// timer1
//
this.timer1.Interval = 2000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// listBox1
//
this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)));
this.listBox1.HorizontalScrollbar = true;
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(88, 178);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(186, 280);
this.listBox1.TabIndex = 5;
this.listBox1.DoubleClick += new System.EventHandler(this.listBox1_DoubleClick);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(6, 94);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(73, 17);
this.label1.TabIndex = 0;
this.label1.Text = "IP Address:";
//
// txtIP
//
this.txtIP.Location = new System.Drawing.Point(88, 92);
this.txtIP.Name = "txtIP";
this.txtIP.Size = new System.Drawing.Size(106, 21);
this.txtIP.TabIndex = 1;
this.txtIP.Text = "";
//
// txtUserName
//
this.txtUserName.Location = new System.Drawing.Point(88, 118);
this.txtUserName.Name = "txtUserName";
this.txtUserName.Size = new System.Drawing.Size(106, 21);
this.txtUserName.TabIndex = 2;
this.txtUserName.Text = "";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(6, 120);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(66, 17);
this.label2.TabIndex = 0;
this.label2.Text = "LoginName:";
//
// txtPassWord
//
this.txtPassWord.Location = new System.Drawing.Point(88, 144);
this.txtPassWord.Name = "txtPassWord";
this.txtPassWord.PasswordChar = '*';
this.txtPassWord.Size = new System.Drawing.Size(106, 21);
this.txtPassWord.TabIndex = 3;
this.txtPassWord.Text = "";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(6, 146);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(60, 17);
this.label3.TabIndex = 0;
this.label3.Text = "PassWord:";
//
// btSure
//
this.btSure.BackColor = System.Drawing.Color.Green;
this.btSure.ForeColor = System.Drawing.Color.Yellow;
this.btSure.Location = new System.Drawing.Point(198, 96);
this.btSure.Name = "btSure";
this.btSure.Size = new System.Drawing.Size(80, 30);
this.btSure.TabIndex = 4;
this.btSure.Text = "Connect(&S)";
this.btSure.Click += new System.EventHandler(this.btSure_Click);
//
// txtMac
//
this.txtMac.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.txtMac.Location = new System.Drawing.Point(88, 512);
this.txtMac.Name = "txtMac";
this.txtMac.ReadOnly = true;
this.txtMac.Size = new System.Drawing.Size(186, 21);
this.txtMac.TabIndex = 0;
this.txtMac.Text = "";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(6, 180);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(66, 17);
this.label4.TabIndex = 0;
this.label4.Text = "SelectMAC:";
//
// label5
//
this.label5.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(6, 516);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(79, 17);
this.label5.TabIndex = 0;
this.label5.Text = "SelectedMAC:";
//
// label6
//
this.label6.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(6, 546);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(66, 17);
this.label6.TabIndex = 0;
this.label6.Text = "Kb/Second:";
//
// btCancel
//
this.btCancel.BackColor = System.Drawing.Color.Red;
this.btCancel.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btCancel.ForeColor = System.Drawing.Color.Blue;
this.btCancel.Location = new System.Drawing.Point(198, 134);
this.btCancel.Name = "btCancel";
this.btCancel.Size = new System.Drawing.Size(80, 30);
this.btCancel.TabIndex = 6;
this.btCancel.Text = "Cancel(&C)";
this.btCancel.Click += new System.EventHandler(this.btCancel_Click);
//
// panel1
//
this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.panel1.Location = new System.Drawing.Point(290, 10);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(494, 532);
this.panel1.TabIndex = 7;
//
// groupBox1
//
this.groupBox1.Controls.Add(this.btFanTi);
this.groupBox1.Controls.Add(this.btEnglish);
this.groupBox1.Controls.Add(this.btJianTi);
this.groupBox1.ForeColor = System.Drawing.Color.Blue;
this.groupBox1.Location = new System.Drawing.Point(4, 10);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(274, 76);
this.groupBox1.TabIndex = 8;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "语言选择(Choose Language)";
//
// btFanTi
//
this.btFanTi.ForeColor = System.Drawing.SystemColors.ControlText;
this.btFanTi.Location = new System.Drawing.Point(105, 36);
this.btFanTi.Name = "btFanTi";
this.btFanTi.Size = new System.Drawing.Size(64, 24);
this.btFanTi.TabIndex = 12;
this.btFanTi.Text = "中文繁体";
this.btFanTi.Click += new System.EventHandler(this.btFanTi_Click);
//
// btEnglish
//
this.btEnglish.ForeColor = System.Drawing.SystemColors.ControlText;
this.btEnglish.Location = new System.Drawing.Point(188, 36);
this.btEnglish.Name = "btEnglish";
this.btEnglish.Size = new System.Drawing.Size(64, 24);
this.btEnglish.TabIndex = 11;
this.btEnglish.Text = "English";
this.btEnglish.Click += new System.EventHandler(this.btEnglish_Click);
//
// btJianTi
//
this.btJianTi.ForeColor = System.Drawing.SystemColors.ControlText;
this.btJianTi.Location = new System.Drawing.Point(22, 36);
this.btJianTi.Name = "btJianTi";
this.btJianTi.Size = new System.Drawing.Size(64, 24);
this.btJianTi.TabIndex = 10;
this.btJianTi.Text = "中文简体";
this.btJianTi.Click += new System.EventHandler(this.btJianTi_Click);
//
// label7
//
this.label7.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.label7.AutoSize = true;
this.label7.Location = new System.Drawing.Point(6, 482);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(79, 17);
this.label7.TabIndex = 10;
this.label7.Text = "MachineName:";
//
// txtMachineName
//
this.txtMachineName.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.txtMachineName.Location = new System.Drawing.Point(88, 478);
this.txtMachineName.Name = "txtMachineName";
this.txtMachineName.ReadOnly = true;
this.txtMachineName.Size = new System.Drawing.Size(186, 21);
this.txtMachineName.TabIndex = 0;
this.txtMachineName.Text = "";
//
// label8
//
this.label8.AutoSize = true;
this.label8.ForeColor = System.Drawing.Color.Blue;
this.label8.Location = new System.Drawing.Point(446, 548);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(165, 17);
this.label8.TabIndex = 0;
this.label8.Text = "上海易可思信息技术有限公司";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(790, 571);
this.Controls.Add(this.label7);
this.Controls.Add(this.txtMachineName);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.panel1);
this.Controls.Add(this.btCancel);
this.Controls.Add(this.label6);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.txtMac);
this.Controls.Add(this.btSure);
this.Controls.Add(this.txtPassWord);
this.Controls.Add(this.label3);
this.Controls.Add(this.txtUserName);
this.Controls.Add(this.label2);
this.Controls.Add(this.txtIP);
this.Controls.Add(this.label1);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.label8);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "网络流量监控系统";
this.Load += new System.EventHandler(this.Form1_Load);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void timer1_Tick(object sender, System.EventArgs e)
{
try
{
this.textBox2.Text=Convert.ToString(this.per.NextValue()/1024f);
this.panel1.Refresh();
}
catch(Exception eee)
{
this.timer1.Enabled=false;
MessageBox.Show("/n"+macname+"/n"+eee.Message);
}
}
private void listBox1_DoubleClick(object sender, System.EventArgs e)
{
try
{
if (this.listBox1.SelectedItem.ToString().Trim()!="")
{
macname=this.listBox1.SelectedItem.ToString();
per=new System.Diagnostics.PerformanceCounter();
((System.ComponentModel.ISupportInitialize)(this.per)).BeginInit();
this.per.CategoryName = "Network Interface";
this.per.CounterName = "Bytes Received/sec";
this.per.InstanceName = macname;
this.per.MachineName = systemname;
this.timer1.Enabled=true;
}
}
catch
{
return;
}
}
private bool run(string ip,string username,string password)
{
try
{
this.listBox1.Items.Clear();
string strsearch=string.Format(@"select description,systemname from Win32_NetworkAdapter where macaddress<>null and manufacturer<>'Microsoft'");
System.Management.ManagementObjectSearcher msearch=new ManagementObjectSearcher();
if (ip.Trim()=="")
{
msearch.Scope=new ManagementScope(@"root/CIMV2");
}
else if (username.Trim()=="")
{
msearch.Scope=new ManagementScope(@"//"+ip+@"/root/CIMV2");
}
else
{
msearch.Scope=new ManagementScope(@"//"+ip+@"/root/CIMV2");
msearch.Scope.Options.Username=username;
msearch.Scope.Options.Password=password;
}
msearch.Query=new ObjectQuery(strsearch);
if((msearch.Get().Count<1) || (msearch.Get()==null))
{
this.timer1.Enabled=false;
this.textBox2.Text="No MAC Address!";
return false;
}
else
{
foreach(System.Management.ManagementObject mymac in msearch.Get())
{
string tempval=mymac["description"].ToString().Trim().Replace("/","_");
tempval=tempval.Replace("//","_");
tempval=tempval.Replace("#","_");
tempval=tempval.Replace("(","[");
tempval=tempval.Replace(")","]");
this.listBox1.Items.Add(tempval);
this.systemname=mymac["systemname"].ToString().Trim();
}
if (this.txtIP.Text.Trim()!="")
{
systemname=txtIP.Text.Trim();
}
}
this.macname=this.listBox1.Items[0].ToString();
per=new System.Diagnostics.PerformanceCounter();
((System.ComponentModel.ISupportInitialize)(this.per)).BeginInit();
this.per.CategoryName = "Network Interface";
this.per.CounterName = "Bytes Received/sec";
this.per.InstanceName = macname;
this.per.MachineName = systemname;
return true;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
private void btSure_Click(object sender, System.EventArgs e)
{
try
{
this.btSure.FlatStyle=System.Windows.Forms.FlatStyle.Popup;
this.btCancel.FlatStyle=System.Windows.Forms.FlatStyle.Standard;
if (run(this.txtIP.Text.Trim(),this.txtUserName.Text.Trim(),this.txtPassWord.Text.Trim()))
{
this.txtMachineName.Text=per.MachineName;
}
else
{
this.timer1.Enabled=false;
return;
}
}
catch(Exception eee)
{
this.timer1.Enabled=false;
MessageBox.Show(eee.Message);
}
}
private void btCancel_Click(object sender, System.EventArgs e)
{
this.btSure.FlatStyle=System.Windows.Forms.FlatStyle.Standard;
this.btCancel.FlatStyle=System.Windows.Forms.FlatStyle.Popup;
this.listBox1.Items.Clear();
this.timer1.Enabled=false;
panel1.Refresh();
for (int i=0;i<dynamicx.Length;i++)
{
dynamicx[i]=-1f;
dynamicy[i]=-1f;
}
}
private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
try
{
RectangleF Rect;
SolidBrush B = new SolidBrush(Color.Green);
StringFormat Ll=new StringFormat();
Ll.Alignment=System.Drawing.StringAlignment.Near;
System.Drawing.Pen insideLine=new Pen(Color.Green,0.2F);
int j=0;
for (int i=panel1.Height-20;i>=0;i=i-20)
{
e.Graphics.DrawLine(insideLine,new Point(0,i),new Point(panel1.Width,i));
Rect=new RectangleF(0,i,80f,80f);
e.Graphics.DrawString(j.ToString()+"kb",new Font("Arial",8),B,Rect,Ll);
j=j+20;
}
for (int i=20;i<=panel1.Width;i=i+20)
{
e.Graphics.DrawLine(insideLine,new Point(i,0),new Point(i,panel1.Height));
}
if (timer1.Enabled==false)
{
return;
}
System.Drawing.Pen dynamicLine=new Pen(Color.Yellow,0.2F);
System.Drawing.Pen firstLine=new Pen(Color.Red,0.2F);
if (dynamicx[dynamicx.Length-1]!=-1f)
{
for (int i=dynamicx.Length-1;i>=0;i--)
{
if (i==0)
{
dynamicx[i]=20f;
dynamicy[i]=(float)this.panel1.Height-20f-this.per.NextValue()/(float)1024;
if (dynamicy[i]>=(float)this.panel1.Height-20f)
{
dynamicy[i]=(float)this.panel1.Height-20f;
}
break;
}
dynamicx[i]=dynamicx[i-1]+20f;
dynamicy[i]=dynamicy[i-1];
}
}
for (int i=0;i<dynamicx.Length;i++)
{
if (dynamicx[i]==-1f)
{
dynamicx[i]=(float)i*(float)20+20f;
dynamicy[i]=(float)this.panel1.Height-20f-this.per.NextValue()/(float)1024;
if (dynamicy[i]<=0)
{
dynamicy[i]=0;
}
if (i==0)
{
e.Graphics.DrawLine(dynamicLine,dynamicx[i],dynamicy[i],dynamicx[i],dynamicy[i]);
}
else
{
e.Graphics.DrawLine(dynamicLine,dynamicx[i-1],dynamicy[i-1],dynamicx[i],dynamicy[i]);
}
return;
}
if (i==0)
{
e.Graphics.DrawLine(dynamicLine,dynamicx[i],dynamicy[i],dynamicx[i],dynamicy[i]);
}
else if(i==1&&dynamicx[dynamicx.Length-1]!=-1)
{
e.Graphics.DrawLine(firstLine,dynamicx[i-1],dynamicy[i-1],dynamicx[i],dynamicy[i]);
}
else
{
e.Graphics.DrawLine(dynamicLine,dynamicx[i-1],dynamicy[i-1],dynamicx[i],dynamicy[i]);
}
}
}
catch(Exception eee)
{
MessageBox.Show(eee.Message+"/n"+eee.StackTrace);
}
}
private void Form1_Load(object sender, System.EventArgs e)
{
//App.SetButtonImage(this,"");
btJianTi_Click(null,null);
panel1.BackColor = Color.Black;
// Connect the Paint event of the PictureBox to the event handling method.
panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
dynamicx=new float[(this.panel1.Width-20)/20];
dynamicy=new float[(this.panel1.Width-20)/20];
for (int i=0;i<dynamicx.Length;i++)
{
dynamicx[i]=-1f;
dynamicy[i]=-1f;
}
// Add the PictureBox control to the Form.
}
private void btJianTi_Click(object sender, System.EventArgs e)
{
this.label1.Text="IP地址:";
this.label2.Text="登录名:";
this.label3.Text="登录密码:";
this.label4.Text="选择网卡:";
this.label5.Text="已选择网卡:";
this.label6.Text="千字节/秒:";
this.label7.Text="机器名称:";
this.btSure.Text="连接(&S)";
this.btCancel.Text="取消(&C)";
}
private void btFanTi_Click(object sender, System.EventArgs e)
{
this.label1.Text="IP地阯:";
this.label2.Text="登錄名:";
this.label3.Text="登錄密碼:";
this.label4.Text="選擇網卡:";
this.label5.Text="已選擇網卡:";
this.label6.Text="仟字節/秒:";
this.label7.Text="機器名稱:";
this.btSure.Text="連接(&S)";
this.btCancel.Text="取消(&C)";
}
private void btEnglish_Click(object sender, System.EventArgs e)
{
this.label1.Text="IP Address:";
this.label2.Text="LoginName:";
this.label3.Text="PassWord:";
this.label4.Text="SelectMAC:";
this.label5.Text="SelectedMAC:";
this.label6.Text="Kb/Second:";
this.label7.Text="MachineName:";
this.btSure.Text="Connect(&S)";
this.btCancel.Text="Cancel(&C)";
}
}
}