提示:
XP使用的是:devcon.exe
win7使用的是:netsh命令
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.IO; //using System.Drawing; using System.Linq; using System.Management; using System.Net.NetworkInformation; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace 自动启用网卡 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Thread tdNetWork=null; private void Form1_Load(object sender, EventArgs e) { StreamReader sr=new StreamReader("C:\\PCI.txt"); string line; if((line = sr.ReadLine()) != null) { tbNetWorkName.Text = line; } sr.Close(); GetPlatForm();//获取操作系统平台 } //获取系统版本 private void GetPlatForm() { OperatingSystem os = Environment.OSVersion; switch (os.Platform) { case PlatformID.Win32Windows: switch (os.Version.Minor) { case 0: labelOS.Text = "Windows 95"; break; case 10: if (os.Version.Revision.ToString() == "2222A ") labelOS.Text = "Windows 98 第二版"; else labelOS.Text = "Windows 98"; break; case 90: labelOS.Text = "Windows Me"; break; } break; case PlatformID.Win32NT: switch (os.Version.Major) { case 3: labelOS.Text = "Windows NT 3.51"; break; case 4: labelOS.Text = "Windows NT 4.0"; break; case 5: switch (os.Version.Minor) { case 0: labelOS.Text = "Windows 2000"; break; case 1: labelOS.Text = "Windows XP"; break; case 2: labelOS.Text = "Windows 2003"; break; } break; case 6: switch (os.Version.Minor) { case 0: labelOS.Text = "Windows Vista"; break; case 1: labelOS.Text = "Windows 7"; break; } break; } break; } } private void btnStop_Click(object sender, EventArgs e) { if (tdNetWork.IsAlive) { tdNetWork.Abort(); } } private void btnStart_Click(object sender, EventArgs e) { if (labelOS.Text == "Windows 7") { if (tbNetWorkName.Text == null) { MessageBox.Show("请填写好网卡名"); tbNetWorkName.Focus(); } } else { if (tbNetWorkName.Text == null) { MessageBox.Show("请填写好设备ID"); tbNetWorkName.Focus(); } } if (tdNetWork == null) { tdNetWork = new Thread(new ParameterizedThreadStart(startNetWork)); tdNetWork.IsBackground = true; tdNetWork.Start(tbNetWorkName.Text); } } //启用网卡 private void startNetWork(object NetWorkName) { while (true) { if (labelOS.Text == "Windows 7") { Execute(string.Format("netsh interface set interface \"{0}\" enabled", NetWorkName), 10); } else { Execute(string.Format("devcon enable PCI\\{0}*", NetWorkName), 10); } Thread.Sleep(5000); } } /// <summary> /// 执行DOS命令,返回DOS命令的输出 /// </summary> /// <param name="dosCommand">dos命令</param> /// <param name="milliseconds">等待命令执行的时间(单位:毫秒), /// 如果设定为0,则无限等待</param> /// <returns>返回DOS命令的输出</returns> public static string Execute(string command, int seconds) { string output = ""; //输出字符串 if (command != null && !command.Equals("")) { Process process = new Process();//创建进程对象 ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = "cmd.exe";//设定需要执行的命令 startInfo.Arguments = "/C " + command;//“/C”表示执行完命令后马上退出 startInfo.UseShellExecute = false;//不使用系统外壳程序启动 startInfo.RedirectStandardInput = false;//不重定向输入 startInfo.RedirectStandardOutput = true; //重定向输出 startInfo.CreateNoWindow = true;//不创建窗口 process.StartInfo = startInfo; try { if (process.Start())//开始进程 { if (seconds == 0) { process.WaitForExit();//这里无限等待进程结束 } else { process.WaitForExit(seconds); //等待进程结束,等待时间为指定的毫秒 } output = process.StandardOutput.ReadToEnd();//读取进程的输出 } } catch { } finally { if (process != null) process.Close(); } } return output; } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { FileStream fs = new FileStream("C:\\PCI.txt", FileMode.Create); //获得字节数组 byte[] data = new UTF8Encoding().GetBytes(tbNetWorkName.Text); //开始写入 fs.Write(data, 0, data.Length); //清空缓冲区、关闭流 fs.Flush(); fs.Close(); } } }
参考文章:http://blog.chinaunix.net/uid-14388574-id-150699.html
原创文章,转载请注明出处:http://www.cnblogs.com/hongfei/archive/2013/04/15/3022708.html