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

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

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

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

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

查看: 4088|回复: 4

[其他] XP下启用网卡的命令?

[复制链接]
发表于 2013-4-15 13:19:58 | 显示全部楼层 |阅读模式
xp下使用: netsh interface set interface "本地连接" "disabled"
会出现错误:
没有指定一个或多个关键性参数
此命令提供的语法不正确。请查看帮助以获取正确的语法信息。
--------------------------------------------------------------------------------
按照网上的解决办法,也行不通
请问还有其他办法吗?


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
 楼主| 发表于 2013-4-15 19:44:31 | 显示全部楼层
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.IO;
  7. //using System.Drawing;
  8. using System.Linq;
  9. using System.Management;
  10. using System.Net.NetworkInformation;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using System.Windows.Forms;

  15. namespace 自动启用网卡
  16. {
  17.     public partial class Form1 : Form
  18.     {
  19.         public Form1()
  20.         {
  21.             InitializeComponent();
  22.         }
  23.         Thread tdNetWork=null;
  24.         private void Form1_Load(object sender, EventArgs e)
  25.         {
  26.             StreamReader sr=new StreamReader("C:\\PCI.txt");
  27.             string line;
  28.             if((line = sr.ReadLine()) != null)
  29.             {
  30.                 tbNetWorkName.Text = line;
  31.             }
  32.             sr.Close();
  33.             GetPlatForm();//获取操作系统平台
  34.         }
  35.         //获取系统版本
  36.         private void GetPlatForm()
  37.         {
  38.             OperatingSystem os = Environment.OSVersion;
  39.             switch (os.Platform)
  40.             {
  41.                 case PlatformID.Win32Windows:
  42.                     switch (os.Version.Minor)
  43.                     {
  44.                         case 0:
  45.                             labelOS.Text = "Windows 95";
  46.                             break;
  47.                         case 10:
  48.                             if (os.Version.Revision.ToString() == "2222A ")
  49.                                 labelOS.Text = "Windows 98 第二版";
  50.                             else
  51.                                 labelOS.Text = "Windows 98";
  52.                             break;
  53.                         case 90:
  54.                             labelOS.Text = "Windows Me";
  55.                             break;
  56.                     }
  57.                     break;
  58.                 case PlatformID.Win32NT:
  59.                     switch (os.Version.Major)
  60.                     {
  61.                         case 3:
  62.                             labelOS.Text = "Windows NT 3.51";
  63.                             break;
  64.                         case 4:
  65.                             labelOS.Text = "Windows NT 4.0";
  66.                             break;
  67.                         case 5:
  68.                             switch (os.Version.Minor)
  69.                             {
  70.                                 case 0:
  71.                                     labelOS.Text = "Windows 2000";
  72.                                     break;
  73.                                 case 1:
  74.                                     labelOS.Text = "Windows XP";
  75.                                     break;
  76.                                 case 2:
  77.                                     labelOS.Text = "Windows 2003";
  78.                                     break;
  79.                             }
  80.                             break;
  81.                         case 6:
  82.                             switch (os.Version.Minor)
  83.                             {
  84.                                 case 0:
  85.                                     labelOS.Text = "Windows Vista";
  86.                                     break;
  87.                                 case 1:
  88.                                     labelOS.Text = "Windows 7";
  89.                                     break;
  90.                             }
  91.                             break;
  92.                     }
  93.                     break;
  94.             }
  95.         }      

  96.         private void btnStop_Click(object sender, EventArgs e)
  97.         {
  98.             if (tdNetWork.IsAlive)
  99.             {
  100.                 tdNetWork.Abort();
  101.             }
  102.         }

  103.         private void btnStart_Click(object sender, EventArgs e)
  104.         {
  105.             if (labelOS.Text == "Windows 7")
  106.             {
  107.                 if (tbNetWorkName.Text == null)
  108.                 {
  109.                     MessageBox.Show("请填写好网卡名");
  110.                     tbNetWorkName.Focus();
  111.                 }
  112.             }
  113.             else
  114.             {
  115.                 if (tbNetWorkName.Text == null)
  116.                 {
  117.                     MessageBox.Show("请填写好设备ID");
  118.                     tbNetWorkName.Focus();
  119.                 }
  120.             }
  121.             if (tdNetWork == null)
  122.             {
  123.                 tdNetWork = new Thread(new ParameterizedThreadStart(startNetWork));
  124.                 tdNetWork.IsBackground = true;
  125.                 tdNetWork.Start(tbNetWorkName.Text);
  126.             }
  127.         }
  128.         //启用网卡
  129.         private void startNetWork(object NetWorkName)
  130.         {
  131.             while (true)
  132.             {
  133.                 if (labelOS.Text == "Windows 7")
  134.                 {
  135.                     Execute(string.Format("netsh interface set interface "{0}" enabled", NetWorkName), 10);
  136.                 }
  137.                 else
  138.                 {
  139.                     Execute(string.Format("devcon enable PCI\\{0}*", NetWorkName), 10);
  140.                 }
  141.                 Thread.Sleep(5000);
  142.             }
  143.         }

  144.         /// <summary>  
  145.         /// 执行DOS命令,返回DOS命令的输出  
  146.         /// </summary>  
  147.         /// <param name="dosCommand">dos命令</param>  
  148.         /// <param name="milliseconds">等待命令执行的时间(单位:毫秒),  
  149.         /// 如果设定为0,则无限等待</param>  
  150.         /// <returns>返回DOS命令的输出</returns>  
  151.         public static string Execute(string command, int seconds)
  152.         {
  153.             string output = ""; //输出字符串  
  154.             if (command != null && !command.Equals(""))
  155.             {
  156.                 Process process = new Process();//创建进程对象  
  157.                 ProcessStartInfo startInfo = new ProcessStartInfo();
  158.                 startInfo.FileName = "cmd.exe";//设定需要执行的命令  
  159.                 startInfo.Arguments = "/C " + command;//“/C”表示执行完命令后马上退出  
  160.                 startInfo.UseShellExecute = false;//不使用系统外壳程序启动  
  161.                 startInfo.RedirectStandardInput = false;//不重定向输入  
  162.                 startInfo.RedirectStandardOutput = true; //重定向输出
  163.                 startInfo.CreateNoWindow = true;//不创建窗口
  164.                 process.StartInfo = startInfo;
  165.                 try
  166.                 {
  167.                     if (process.Start())//开始进程  
  168.                     {
  169.                         if (seconds == 0)
  170.                         {
  171.                             process.WaitForExit();//这里无限等待进程结束  
  172.                         }
  173.                         else
  174.                         {
  175.                             process.WaitForExit(seconds); //等待进程结束,等待时间为指定的毫秒  
  176.                         }
  177.                         output = process.StandardOutput.ReadToEnd();//读取进程的输出  
  178.                     }
  179.                 }
  180.                 catch
  181.                 {
  182.                 }
  183.                 finally
  184.                 {
  185.                     if (process != null)
  186.                         process.Close();
  187.                 }
  188.             }
  189.             return output;
  190.         }

  191.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  192.         {
  193.             FileStream fs = new FileStream("C:\\PCI.txt", FileMode.Create);
  194.             //获得字节数组
  195.             byte[] data = new UTF8Encoding().GetBytes(tbNetWorkName.Text);
  196.             //开始写入
  197.             fs.Write(data, 0, data.Length);
  198.             //清空缓冲区、关闭流
  199.             fs.Flush();
  200.             fs.Close();
  201.         }
  202.     }
  203. }
复制代码
原文地址:http://www.cnblogs.com/hongfei/archive/2013/04/15/3022708.html
发表于 2013-4-15 19:48:54 | 显示全部楼层
解决了吗?
 楼主| 发表于 2013-4-15 21:49:14 | 显示全部楼层
恩,暂时使用这方法解决了
发表于 2013-4-15 22:17:46 | 显示全部楼层
在c#里直接执行cmd命令就简单多了
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

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

GMT+8, 2024-5-5 05:25

© 2014-2021

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