C#中禁用和開啟網(wǎng)卡
當前位置:點晴教程→知識管理交流
→『 技術(shù)文檔交流 』
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.
/// 網(wǎng)卡列表 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.
/// 禁用網(wǎng)卡 42.
/// </summary>5 43.
/// <param
name="netWorkName">網(wǎng)卡名</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.
/// 啟用網(wǎng)卡 60.
/// </summary> 61.
/// <param
name="netWorkName">網(wǎng)卡名</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.
/// 網(wǎng)卡狀態(tài) 79.
/// </summary> 80.
/// <param
name="netWorkName">網(wǎng)卡名</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.
/// 得到指定網(wǎng)卡 99.
/// </summary> 100.
/// <param
name="networkname">網(wǎng)卡名字</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("開啟網(wǎng)卡失敗!"); 128.
} 129.
else 130.
{ 131.
MessageBox.Show("開啟網(wǎng)卡成功!"); 132.
} 133.
} 134.
else 135.
{ 136.
MessageBox.Show("網(wǎng)卡己開啟!"); 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("禁用網(wǎng)卡失敗!"); 150.
} 151.
else 152.
{ 153.
MessageBox.Show("禁用網(wǎng)卡成功!"); 154.
} 155.
} 156.
else 157.
{ 158.
MessageBox.Show("網(wǎng)卡己禁用!"); 159.
} 160.
161.
NetWorkList(); 162.
} 163.
164.
} 165.
} 該文章在 2021/5/21 17:25:04 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |