C#中禁用和开启网卡
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
1.
using
System; 2.
using
System.Collections.Generic; 3.
using
System.ComponentModel; 4.
using
System.Data; 5.
using
System.Drawing; 6.
using
System.Text; 7.
using
System.Windows.Forms; 8.
using
System.Management; 9.
using
System.Collections; 10. 11.namespace NetWorkControl 12.{ 13.
public partial class Form1 : Form 14.
{ 15.
public Form1() 16.
{ 17. InitializeComponent(); 18. NetWorkList(); 19.
} 20. 21.
/// <summary> 22.
/// 网卡列表 23.
/// </summary> 24.
public void NetWorkList() 25.
{ 26. string manage = "SELECT * From
Win32_NetworkAdapter"; 27. ManagementObjectSearcher searcher =
new ManagementObjectSearcher(manage); 28. ManagementObjectCollection
collection = searcher.Get(); 29.
List<string> netWorkList = new List<string>(); 30. 31. foreach (ManagementObject obj in collection) 32. { 33. netWorkList.Add(obj["Name"].ToString()); 34. 35. } 36. this.cmbNetWork.DataSource = netWorkList; 37. 38.
} 39. 40.
/// <summary> 41.
/// 禁用网卡 42.
/// </summary>5 43.
/// <param
name="netWorkName">网卡名</param> 44.
/// <returns></returns> 45.
public bool DisableNetWork(ManagementObject network) 46.
{ 47. try 48. { 49. network.InvokeMethod("Disable", null); 50. return true; 51. } 52. catch 53. { 54. return false; 55. } 56.
} 57. 58.
/// <summary> 59.
/// 启用网卡 60.
/// </summary> 61.
/// <param
name="netWorkName">网卡名</param> 62.
/// <returns></returns> 63.
public bool EnableNetWork(ManagementObject network) 64.
{ 65. try 66. { 67. network.InvokeMethod("Enable", null ); 68. return true; 69. } 70. catch 71. { 72. return false; 73. } 74. 75.
} 76. 77.
/// <summary> 78.
/// 网卡状态 79.
/// </summary> 80.
/// <param
name="netWorkName">网卡名</param> 81.
/// <returns></returns> 82.
public bool NetWorkState(string netWorkName) 83.
{ 84. string netState = "SELECT * From
Win32_NetworkAdapter"; 85. ManagementObjectSearcher searcher =
new ManagementObjectSearcher(netState); 86. ManagementObjectCollection
collection = searcher.Get(); 87. foreach (ManagementObject manage in collection) 88. { 89. if (manage["Name"].ToString() == netWorkName ) 90. { 91. return true; 92. } 93. } 94. return false; 95.
} 96. 97.
/// <summary> 98.
/// 得到指定网卡 99.
/// </summary> 100.
/// <param
name="networkname">网卡名字</param> 101.
/// <returns></returns> 102.
public ManagementObject NetWork(string networkname) 103.
{ 104.
string netState = "SELECT * From
Win32_NetworkAdapter"; 105.
106.
ManagementObjectSearcher
searcher = new ManagementObjectSearcher(netState); 107.
ManagementObjectCollection
collection = searcher.Get(); 108.
109.
foreach (ManagementObject manage in collection) 110.
{ 111.
if (manage["Name"].ToString() == networkname) 112.
{ 113.
return manage; 114.
} 115.
} 116.
117.
118.
return null; 119.
} 120.
121.
private void btnStart_Click(object sender, EventArgs e) 122.
{ 123.
if (NetWorkState(this.cmbNetWork.SelectedValue.ToString())) 124.
{ 125.
if (!EnableNetWork(NetWork(this.cmbNetWork.SelectedValue.ToString()))) 126.
{ 127.
MessageBox.Show("开启网卡失败!"); 128.
} 129.
else 130.
{ 131.
MessageBox.Show("开启网卡成功!"); 132.
} 133.
} 134.
else 135.
{ 136.
MessageBox.Show("网卡己开启!"); 137.
} 138.
139.
NetWorkList(); 140.
} 141.
142.
private void btnStop_Click(object sender, EventArgs e) 143.
{ 144.
145.
if (NetWorkState(this.cmbNetWork.SelectedValue.ToString())) 146.
{ 147.
if (!DisableNetWork(NetWork(this.cmbNetWork.SelectedValue.ToString()))) 148.
{ 149.
MessageBox.Show("禁用网卡失败!"); 150.
} 151.
else 152.
{ 153.
MessageBox.Show("禁用网卡成功!"); 154.
} 155.
} 156.
else 157.
{ 158.
MessageBox.Show("网卡己禁用!"); 159.
} 160.
161.
NetWorkList(); 162.
} 163.
164.
} 165.
} 该文章在 2021/5/21 17:25:04 编辑过 |
关键字查询
相关文章
正在查询... |