http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 7588|回复: 4

[其他] Win8下禁用/启用网卡的C#源代码哪里错了吗?

[复制链接]
发表于 2013-3-20 20:26:47 | 显示全部楼层 |阅读模式

提示:

以下代码在win7下能够成功启用/禁用网卡,但是在win8下无效

  1. /// <summary>
  2.         /// 禁用网卡
  3.         /// </summary>5
  4.         /// <param name="netWorkName">网卡名</param>
  5.         /// <returns></returns>
  6.         private static bool DisableNetWork(ManagementObject network)
  7.         {
  8.             try
  9.             {
  10.                 network.InvokeMethod("Disable", null);
  11.                 return true;
  12.             }
  13.             catch
  14.             {
  15.                 return false;
  16.             }
  17.         }
  18.         /// <summary>
  19.         /// 启用网卡
  20.         /// </summary>
  21.         /// <param name="netWorkName">网卡名</param>
  22.         /// <returns></returns>
  23.         private static bool EnableNetWork(ManagementObject network)
  24.         {
  25.             try
  26.             {
  27.                 network.InvokeMethod("Enable", null);
  28.                 return true;
  29.             }
  30.             catch
  31.             {
  32.                 return false;
  33.             }
  34.         }
复制代码


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
 楼主| 发表于 2013-3-20 22:23:51 | 显示全部楼层
到现在还没找出问题所在,执行以上代码也不会报错,
目前是使用dos命令来启用/禁用网卡;
netsh interface set interface "Wi-Fi" disabled//禁用网卡
netsh interface set interface "Wi-Fi" enabled//启用网卡
这种方法也可以达到重启网卡的效果,但是通过wmi的方式不行,期待高手解答哈~
发表于 2013-3-20 22:33:08 | 显示全部楼层
marcofly 发表于 2013-3-20 22:23
到现在还没找出问题所在,执行以上代码也不会报错,
目前是使用dos命令来启用/禁用网卡;
netsh interfac ...

http://www.sufeinet.com/thread-2177-1-1.html 可以试试Cmd的方式,我今天刚写的?Cmd执行方法
发表于 2013-3-20 22:37:46 | 显示全部楼层
  1. 方法一:使用netsh

  2. rem 适用于Win2003

  3. rem 把Win2003的ifmon.dll拷贝到WinXP以后WinXP也可以用

  4. rem 禁用网卡

  5. netsh interface set interface name="本地连接" admin=DISABLED

  6. rem 启用网卡

  7. netsh interface set interface name="本地连接" admin=ENABLED

  8. 方法二:使用devcon.exe

  9. @echo off

  10. rem 需要devcon.exe的支持

  11. echo 请输入你的选择:(禁用网卡为N,启用为Y)

  12. set/p SELECT=

  13. if /i %SELECT%==Y set SELECT=ENABLE&GOTO :DO

  14. if /i %SELECT%==N set SELECT=DISABLE

  15. :DO

  16. for /f "tokens=2 delims=&" %%i in ('devcon find pci\* ^| findstr "Fast Ethernet"') do devcon %SELECT% *%%i*>NUL

  17. 方法三:使用VBScript

  18. Const ssfCONTROLS = 3  

  19. sConnectionName = "本地连接"  '可改成需要控制的连接名称,如"无线网络连接"等  

  20. sEnableVerb = "启用(&A)"  

  21. sDisableVerb = "停用(&B)"    非xp 系统可能 是禁用

  22. set shellApp = createobject("shell.application")  

  23. set oControlPanel = shellApp.Namespace(ssfCONTROLS)  

  24. set oNetConnections = nothing  

  25. for each folderitem in oControlPanel.items  

  26. if folderitem.name  = "网络连接" then  

  27.    set oNetConnections = folderitem.getfolder: exit for  

  28. end if  

  29. next  

  30. if oNetConnections is nothing then  

  31. msgbox "未找到网络连接文件夹"  

  32. wscript.quit  

  33. end if  

  34. set oLanConnection = nothing  

  35. for each folderitem in oNetConnections.items  

  36. if lcase(folderitem.name)  = lcase(sConnectionName) then  

  37.    set oLanConnection = folderitem: exit for  

  38. end if  

  39. next  

  40. if oLanConnection is nothing then  

  41. msgbox "未找到 '" & sConnectionName & "' item"  

  42. wscript.quit  

  43. end if  

  44. bEnabled = true  

  45. set oEnableVerb = nothing  

  46. set oDisableVerb = nothing  

  47. s = "Verbs: " & vbcrlf  

  48. for each verb in oLanConnection.verbs  

  49. s = s & vbcrlf & verb.name  

  50. if verb.name = sEnableVerb then   

  51.    set oEnableVerb = verb   

  52.    bEnabled = false  

  53. end if  

  54. if verb.name = sDisableVerb then   

  55.    set oDisableVerb = verb   

  56. end if  

  57. next  

  58. 'debugging displays left just in case...

  59. '  

  60. 'msgbox s ': wscript.quit  

  61. 'msgbox "Enabled: " & bEnabled ': wscript.quit  

  62. 'not sure why, but invokeverb always seemed to work   

  63. 'for enable but not disable.   

  64. '  

  65. 'saving a reference to the appropriate verb object   

  66. 'and calling the DoIt method always seems to work.  

  67. '  

  68. if bEnabled then  

  69. '  oLanConnection.invokeverb sDisableVerb  

  70. oDisableVerb.DoIt  

  71. else  

  72. '  oLanConnection.invokeverb sEnableVerb  

  73. oEnableVerb.DoIt  

  74. end if  

  75. 'adjust the sleep duration below as needed...  

  76. '  

  77. 'if you let the oLanConnection go out of scope  

  78. 'and be destroyed too soon, the action of the verb  

  79. 'may not take...  

  80. '  

  81. wscript.sleep 400


  82. 发现WIin7系统中,开机启动服务中WLAN AutoConfig和 Wired Autoconfig都是开启的
复制代码
我建议你使用第二种我测试过可以的
 楼主| 发表于 2013-3-20 22:44:37 | 显示全部楼层
飞哥很给力哈{:soso_e179:}
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-5-3 04:45

© 2014-2021

快速回复 返回顶部 返回列表