Visual Studio 2010中配置SharpPcap
一.我的编程环境:
win7旗舰版 、VS2010旗舰版、WinPcap4.1.3、SharpPcap4.2.0。
二. 安装及调试步骤:
1.安装Winpcap4.1.3(WinPcap4.1.3下载)
2.解压SharpPcap-4.2.0.bin.zip(SharpPcap4.2.0.bin.zip&&SharpPcap4.2.0.src.zip下载)解压后打开debug文件夹,可以看到里面有两个dll文件,这就是我们程序中要用到的东西。SharpPcap-4.2.0.src.zip压缩包中,包含SharpPcap的所有源代码和一些示例程序。
3.打开VS2010,新建一个C#的控制台项目。
4.然后单击“项目”下拉菜单,选择 “添加引用”,在弹出的对话框中单击 “浏览” 选项卡,然后选择第2步中SharpPcap-4.2.0.bin.zip解压后的路径,然后将debug中的SharpPcap.dll添加进去。
5.将下面的代码,粘贴到你的项目中,测试配置是否成功,如果成功则会显示你的网络适配器的信息。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using SharpPcap; 6 7 namespace SharpPcap1 8 { 9 class Program 10 { 11 public class IfListAdv 12 { 13 /// <summary> 14 /// Obtaining the device list 15 /// </summary> 16 public static void Main(string[] args) 17 { 18 // Print SharpPcap version 19 string ver = SharpPcap.Version.VersionString; 20 Console.WriteLine("SharpPcap {0}, Example1.IfList.cs", ver); 21 22 // Retrieve the device list 23 var devices = CaptureDeviceList.Instance; 24 25 // If no devices were found print an error 26 if (devices.Count < 1) 27 { 28 Console.WriteLine("No devices were found on this machine"); 29 return; 30 } 31 32 Console.WriteLine("\nThe following devices are available on this machine:"); 33 Console.WriteLine("----------------------------------------------------\n"); 34 35 /* Scan the list printing every entry */ 36 foreach (var dev in devices) 37 Console.WriteLine("{0}\n", dev.ToString()); 38 39 Console.Write("Hit \'Enter\' to exit..."); 40 Console.ReadLine(); 41 } 42 } 43 } 44 }
如果配置无误的话,编译运行这段代码会得到如下的结果: